Index: trunk/psModules/src/imcombine/pmSubtractionStamps.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 23780)
+++ trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 23789)
@@ -95,4 +95,6 @@
     if (x < footprint || x >= numCols - footprint || y < footprint || y >= numRows - footprint) {
         clean = false;
+        goto CHECK_STAMP_MASK_DONE;
+
     }
 
@@ -116,11 +118,11 @@
     // Check the immediate pixel
     if (clean && (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & (maskVal | PM_SUBTRACTION_MASK_REJ))) {
+        goto CHECK_STAMP_MASK_DONE;
         clean = false;
     }
 
+    // Check the footprint
     int xMin = PS_MAX(x - footprint, 0), xMax = PS_MIN(x + footprint, numCols - 1); // Bounds in x
     int yMin = PS_MAX(y - footprint, 0), yMax = PS_MIN(y + footprint, numRows - 1); // Bounds in y
-
-    // Check the footprint
     if (clean) {
         for (int j = yMin; j <= yMax; j++) {
@@ -133,6 +135,6 @@
         }
     }
-CHECK_STAMP_MASK_DONE:
-
+
+ CHECK_STAMP_MASK_DONE:
     if (!clean) {
         // Mask the footprint, so we don't select something near it again
@@ -314,6 +316,6 @@
                 for (int y = subRegion->y0; y <= subRegion->y1; y++) {
                     for (int x = subRegion->x0; x <= subRegion->x1; x++) {
-                        if (checkStampMask(x, y, subMask, mode, footprint) &&
-                            image->data.F32[y][x] > fluxStamp) {
+                        if (image->data.F32[y][x] > fluxStamp &&
+                            checkStampMask(x, y, subMask, mode, footprint)) {
                             fluxStamp = image->data.F32[y][x];
                             xStamp = x;
@@ -363,4 +365,34 @@
 }
 
+bool pmSubtractionStampsRegions(pmSubtractionStampList *stamps, const char *filename)
+{
+    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
+    PS_ASSERT_STRING_NON_EMPTY(filename, false);
+
+    FILE *outFile = fopen(filename, "w"); // File to write
+    if (!outFile) {
+        psError(PS_ERR_IO, false, "Unable to open %s for writing");
+        return false;
+    }
+
+    for (int i = 0; i < stamps->num; i++) {
+        // Outline the stamp
+        psRegion *region = stamps->regions->data[i]; // Region of interest
+        fprintf(outFile, "image;box(%f,%f,%f,%f,0)\n",
+                0.5 * (region->x0 + region->x1), 0.5 * (region->y0 + region->y1),
+                region->x1 - region->x0, region->y1 - region->y0);
+
+        psVector *xList = stamps->x->data[i], *yList = stamps->y->data[i]; // Coordinate lists
+
+        for (int j = 0; j < xList->n; j++) {
+            fprintf(outFile, "image;circle(%f,%f,%d)\n",
+                    xList->data.F32[j], yList->data.F32[j], stamps->footprint);
+        }
+    }
+
+    fclose(outFile);
+
+    return true;
+}
 
 pmSubtractionStampList *pmSubtractionStampsSet(const psVector *x, const psVector *y,
Index: trunk/psModules/src/imcombine/pmSubtractionStamps.h
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionStamps.h	(revision 23780)
+++ trunk/psModules/src/imcombine/pmSubtractionStamps.h	(revision 23789)
@@ -124,3 +124,12 @@
     );
 
+
+/// Write a ds9 region file with stamp positions
+///
+/// Intended for debugging
+bool pmSubtractionStampsRegions(pmSubtractionStampList *stamps, ///< Stamps
+                                const char *filename ///< Filename to which to write regions
+    );
+
+
 #endif
