--- a/branches/Gxsm-2x3-transition-sig/plug-ins/math/background/removelineshifts.C
+++ b/branches/Gxsm-2x3-transition-sig/plug-ins/math/background/removelineshifts.C
@@ -379,6 +379,11 @@
 			x1 = x2;
 			x2 = tmp;
 		}
+		if (y1 > y2){
+			int tmp = y1;
+			y1 = y2;
+			y2 = tmp;
+		}
 	}
 
 	int ntimes_tmp = tf-ti+1;
@@ -400,22 +405,23 @@
 			avg_prev1 = 0.;
 			avg_prev2 = 0.;
 			
-			for (int line = 0; line < ny; line++) {
+			for (int line = y1; line < ny; line++) {
 				avg = 0.;
 				SrcZ->SetPtr(0,line);
+
+				if (line < y2){
+					for (int col = x1; col < x2; col++)
+						avg += SrcZ->GetNext();
 				
-				for (int col = x1; col < x2; col++)
-					avg += SrcZ->GetNext();
-				
-				avg /= (x2-x1);
-				avg_delta1 = avg - avg_prev1;
-				avg_delta2 = avg_prev1 - avg_prev2;
-				if ( fabs (avg_delta2 - avg_delta1) > threashold) // 2nd order check
-					Z_adjust += avg_delta1;
-
-				avg_prev2 = avg_prev1;
-				avg_prev1 = avg;
-				
+					avg /= (x2-x1);
+					avg_delta1 = avg - avg_prev1;
+					avg_delta2 = avg_prev1 - avg_prev2;
+					if ( fabs (avg_delta2 - avg_delta1) > threashold) // 2nd order check
+						Z_adjust += avg_delta1;
+
+					avg_prev2 = avg_prev1;
+					avg_prev1 = avg;
+				}
 				SrcZ->SetPtr(0,line);
 				DestZ->SetPtr(0,line);