Index: trunk/psModules/src/imcombine/pmSubtractionMatch.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 14713)
+++ trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 14738)
@@ -214,7 +214,7 @@
     // Putting important variable declarations here, since they are freed after a "goto" if there is an error.
     psImage *subMask = NULL;            // Mask for subtraction
-    psArray *stamps = NULL;             // Stamps for matching PSF
     psRegion *region = NULL;            // Iso-kernel region
     psString regionString = NULL;       // String for region
+    psArray *stamps = NULL;             // Stamps for matching PSF
     psVector *solution = NULL;          // Solution to match PSF
     pmSubtractionKernels *kernels = NULL; // Kernel basis functions
@@ -224,5 +224,4 @@
     if (stampsName && strlen(stampsName) > 0) {
         psTrace("psModules.imcombine", 3, "Reading stamps from %s...\n", stampsName);
-        psVector *xStamp = NULL, *yStamp = NULL; // Stamp positions and fluxes
         if (input) {
             // We have x, y because the target is provided by the input image
@@ -234,10 +233,10 @@
         if (!stampsData) {
             psError(PS_ERR_IO, false, "Unable to read stamps file %s", stampsName);
-            goto ERROR;
+            return false;
         }
 
         // Correct for IRAF/FITS (unit-offset) positions to C (zero-offset) positions
-        xStamp = stampsData->data[0];
-        yStamp = stampsData->data[1];
+        psVector *xStamp = stampsData->data[0]; // x positions
+        psVector *yStamp = stampsData->data[1]; // y positions
         psBinaryOp(xStamp, xStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
         psBinaryOp(yStamp, yStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
@@ -251,35 +250,4 @@
 
     memCheck("mask");
-
-    if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) {
-        if (!getStamps(&stamps, stampsData, reference, input, subMask, weight, NULL,
-                       threshold, stampSpacing, targetWidth, size, footprint)) {
-            goto ERROR;
-        }
-        kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder, optFWHMs, optOrder,
-                                                  stamps, footprint, optThreshold);
-        // XXX This is not quite the best thing to do here--- we'll find the same set of stamps again later,
-        // but since we may not be interested in the entire image on the first pass through, we have to blow
-        // the whole lot away.  The alternative is to mark stamps that are outside the region of interest, and
-        // ignore them when generating sums and rejecting.
-        psFree(stamps);
-        stamps = NULL;
-
-        if (!kernels) {
-            psErrorClear();
-            psWarning("Unable to derive optimum ISIS kernel --- switching to default.");
-        }
-    }
-
-    if (kernels == NULL) {
-        // Not an ISIS/GUNK kernel, or the optimum kernel search failed
-        kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders,
-                                               inner, binning, ringsOrder);
-    }
-
-    psMetadataAddPtr(convolved->analysis, PS_LIST_TAIL, "SUBTRACTION.KERNEL", PS_DATA_UNKNOWN,
-                     "Subtraction kernels", kernels);
-
-    memCheck("kernels");
 
     // Get region of interest
@@ -307,4 +275,27 @@
                         regionString, numCols, numRows);
             }
+
+            // Define kernel basis functions
+            if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) {
+                if (!getStamps(&stamps, stampsData, reference, input, subMask, weight, NULL,
+                               threshold, stampSpacing, targetWidth, size, footprint)) {
+                    goto ERROR;
+                }
+                kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder, optFWHMs, optOrder,
+                                                          stamps, footprint, optThreshold);
+                if (!kernels) {
+                    psErrorClear();
+                    psWarning("Unable to derive optimum ISIS kernel --- switching to default.");
+                }
+            }
+            if (kernels == NULL) {
+                // Not an ISIS/GUNK kernel, or the optimum kernel search failed
+                kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders,
+                                                       inner, binning, ringsOrder);
+            }
+            psMetadataAddPtr(convolved->analysis, PS_LIST_TAIL, "SUBTRACTION.KERNEL",
+                             PS_DATA_UNKNOWN | PS_META_DUPLICATE_OK, "Subtraction kernels", kernels);
+
+            memCheck("kernels");
 
             int numRejected = -1;               // Number of rejected stamps in each iteration
@@ -422,4 +413,6 @@
                 goto ERROR;
             }
+            psFree(kernels);
+            kernels = NULL;
 
             // Put the solution on the metadata
@@ -466,5 +459,4 @@
         goto ERROR;
     }
-    psFree(kernels);
 
     memCheck("convolution");
