diff --git a/pgm.c b/pgm.c
index 5aaccf2ae48b2958c9f217f74fd24777a2136c5a..020e5d2d5cfcb906be4cf603df923e243b74713a 100644
--- a/pgm.c
+++ b/pgm.c
@@ -133,30 +133,24 @@ pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig){
     }
     return success;
 }
-//index error
+
 pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int32_t y0, int32_t y1){
     if (crop == NULL || orig == NULL){
         return failure;
     }
+    if (x0 < 0 || y0 < 0 || x0 > x1 || y0 > y1 || x1 > orig->pixels.l || y1 > orig->pixels.l){
+        return failure;
+    }
+
+
     int l = x1 - x0;
     int c = y1 - y0;
-    int max = l + c/ 2;
-    printf("%d", max);
-    crop->max = max;
+    crop->max = 255;
     matrix_alloc(&crop->pixels, l, c);
-    printf("step");
-    int k = 0;
-    int m = 0;
-    for (int i = 0; i < orig->pixels.l; i++){
-        for (int j = 0; j < orig->pixels.c; j++){  
-            if (i >= x0 && i < x1 && j >= y0 && j < y1){
-                crop->pixels.data[k][m] = orig->pixels.data[i][j];
-                m++;
-                printf("%d %d", k, m);
-            }
-            if (i >= x0 && i < x1){
-               k++;
-            }
+
+    for (int i = x0, k = 0; i < x1; i++, k++){
+        for (int j = y0, m = 0; j < y1; j++, m++){  
+            crop->pixels.data[k][m] = orig->pixels.data[i][j];
         }
     }
     return success;
diff --git a/pgm_main.c b/pgm_main.c
index b76fdb7b202474a3fdb8dc3164e375040992b6ff..2a4e000124a07ee0558e0b3f9fc09fb8d4d5025e 100644
--- a/pgm_main.c
+++ b/pgm_main.c
@@ -30,7 +30,7 @@ int main(){
     printf("%d", pgm_photomaton(&photomatonx16, &photomatonx4));
     //printf("%d", pgm_write_to_file(&photomatonx16, "photomatonx16.pgm"));
 
-    printf("%d", pgm_crop(&cropped, &a, 7, 14, 7, 14));
+    printf("%d", pgm_crop(&cropped, &a, 100, 300, 100, 200));
     printf("%d", pgm_write_to_file(&cropped, "cropped.pgm"));
 
     matrix_destroy(&a.pixels);