Index: /trunk/stac/src/stacRejection.c
===================================================================
--- /trunk/stac/src/stacRejection.c	(revision 2764)
+++ /trunk/stac/src/stacRejection.c	(revision 2765)
@@ -136,43 +136,51 @@
 #ifdef TESTING
 		    rejmap->data.F32[y][x] = maskVal;
-
-		    // Calculate gradient
-		    float meanGrads = 0.0;
-		    int numGrads = 0;
-		    for (int j = 0; j < nImages; j++) {
-			if (i != j) {
-			    // Get coordinates for that image
-			    (void)psPlaneTransformApply(inCoords, inverseMaps->data[j], outCoords);
-			    int xPix = (int)(inCoords->x + 0.5);
-			    int yPix = (int)(inCoords->y + 0.5);
-			    if ((xPix >= 0) && (xPix <= ((psImage*)(inputs->data[j]))->numCols - 1) &&
-				(yPix >= 0) && (yPix <= ((psImage*)(inputs->data[j]))->numRows - 1)) {
-				// Calculate the gradient
-				meanGrads += stacGradient(inputs->data[j], xPix, yPix);
-				numGrads++;
+#endif
+
+		    if (maskVal > config->frac) {
+			// Calculate mean gradient on other images
+			float meanGrads = 0.0;
+			int numGrads = 0;
+			for (int j = 0; j < nImages; j++) {
+			    if (i != j) {
+				// Get coordinates for that image
+				(void)psPlaneTransformApply(inCoords, inverseMaps->data[j], outCoords);
+				int xPix = (int)(inCoords->x + 0.5);
+				int yPix = (int)(inCoords->y + 0.5);
+				if ((xPix >= 0) && (xPix <= ((psImage*)(inputs->data[j]))->numCols - 1) &&
+				    (yPix >= 0) && (yPix <= ((psImage*)(inputs->data[j]))->numRows - 1)) {
+				    // Calculate the gradient
+				    meanGrads += stacGradient(inputs->data[j], xPix, yPix);
+				    numGrads++;
+				}
 			    }
 			}
-		    }
-		    if (numGrads > 0) {
-			meanGrads /= (float)numGrads;
-		    } else {
-			meanGrads = 0;
-		    }
-		    grad->data.F32[y][x] = stacGradient(inputs->data[i], x, y) / meanGrads;
-#endif
-
-		    if ((maskVal > config->frac) &&
-			(stacGradient(inputs->data[i], x, y)) < config->grad * meanGrads) {
-			mask->data.U8[y][x] = 1;
-			nBad++;
+			if (numGrads > 0) {
+			    meanGrads /= (float)numGrads;
+			} else {
+			    meanGrads = 0;
+			}
+
+#ifdef TESTING
+			grad->data.F32[y][x] = stacGradient(inputs->data[i], x, y) / meanGrads;
+#endif
+
+			if (stacGradient(inputs->data[i], x, y) < config->grad * meanGrads) {
+			    mask->data.U8[y][x] = 1;
+			    nBad++;
 #ifdef CRFLUX
-			fprintf(crs, "%d %d --> %f %f %f\n", x, y,
-				((psImage*)(inputs->data[i]))->data.F32[y][x], maskVal,
-				stacGradient(inputs->data[i], x, y));
-#endif
+			    fprintf(crs, "%d %d --> %f %f %f\n", x, y,
+				    ((psImage*)(inputs->data[i]))->data.F32[y][x], maskVal,
+				    stacGradient(inputs->data[i], x, y));
+#endif
+			} else {
+			    mask->data.U8[y][x] = 0;
+			} // Gradient threshold
 		    } else {
 			mask->data.U8[y][x] = 0;
-		    }
-
+		    } // Rejection threshold
+
+		} else {
+		    mask->data.U8[y][x] = 0;
 		} // Only touching pixels of interest
 
