Index: trunk/psModules/src/imcombine/pmSubtractionMatch.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 14801)
+++ trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 14802)
@@ -47,5 +47,4 @@
 
 static bool getStamps(pmSubtractionStampList **stamps, // Stamps to read
-                      const psArray *stampsData, // Stamp data from a file
                       const pmReadout *reference, // Reference readout
                       const pmReadout *input, // Input readout, or NULL to generate fake stamps
@@ -60,35 +59,6 @@
     )
 {
-    if (stampsData && *stamps) {
-        // We've already previously read all the stamps
-        return true;
-    }
-
-    psImage *inImage = NULL; // Input image
-    if (input) {
-        inImage = input->image;
-    }
-
-    if (stampsData) {
-        psVector *xStamp = NULL, *yStamp = NULL, *fluxStamp = NULL; // Stamp positions and fluxes
-        if (input) {
-            // We have x, y because the target is provided by the input image
-            xStamp = stampsData->data[0];
-            yStamp = stampsData->data[1];
-        } else {
-            // We have x, y and flux in order to generate a target
-            xStamp = stampsData->data[0];
-            yStamp = stampsData->data[1];
-            fluxStamp = stampsData->data[2];
-        }
-
-        psFree(*stamps);
-        // Apply exclusion zone if we're matching to a nominated PSF; otherwise don't care
-        *stamps = pmSubtractionSetStamps(xStamp, yStamp, fluxStamp, reference->image, subMask,
-                                         region, stampSpacing, input ? 0 : footprint);
-    }
     psTrace("psModules.imcombine", 3, "Finding stamps...\n");
-    *stamps = pmSubtractionFindStamps(*stamps, reference->image, subMask, region,
-                                      threshold, stampSpacing);
+    *stamps = pmSubtractionStampsFind(*stamps, reference->image, subMask, region, threshold, stampSpacing);
     if (!*stamps) {
         psError(PS_ERR_UNKNOWN, false, "Unable to find stamps.");
@@ -98,11 +68,14 @@
     memCheck("  find stamps");
 
-    if (!input && !pmSubtractionGenerateStamps(*stamps, targetWidth, footprint, size)) {
+    if (!input && !pmSubtractionStampsGenerate(*stamps, targetWidth, footprint, size)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to generate target stamps.");
         return false;
     }
 
+    memCheck("   generate stamps");
+
     psTrace("psModules.imcombine", 3, "Extracting stamps...\n");
-    if (!pmSubtractionExtractStamps(*stamps, reference->image, inImage, weight, footprint, size)) {
+    if (!pmSubtractionStampsExtract(*stamps, reference->image, input ? input->image : NULL,
+                                    weight, footprint, size)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to extract stamps.");
         return false;
@@ -225,27 +198,4 @@
     pmSubtractionKernels *kernels = NULL; // Kernel basis functions
 
-    // Read stamps from file
-    psArray *stampsData = NULL;         // Stamps data read from file
-    if (stampsName && strlen(stampsName) > 0) {
-        psTrace("psModules.imcombine", 3, "Reading stamps from %s...\n", stampsName);
-        if (input) {
-            // We have x, y because the target is provided by the input image
-            stampsData = psVectorsReadFromFile(stampsName, "%f %f"); // Stamp positions
-        } else {
-            // We have x, y and flux in order to generate a target
-            stampsData = psVectorsReadFromFile(stampsName, "%f %f %f"); // Stamp positions
-        }
-        if (!stampsData) {
-            psError(PS_ERR_IO, false, "Unable to read stamps file %s", stampsName);
-            return false;
-        }
-
-        // Correct for IRAF/FITS (unit-offset) positions to C (zero-offset) positions
-        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));
-    }
-
     int numCols = reference->image->numCols, numRows = reference->image->numRows; // Image dimensions
 
@@ -281,7 +231,42 @@
             }
 
+            // Read stamps from file
+            if (stampsName && strlen(stampsName) > 0) {
+                psTrace("psModules.imcombine", 3, "Reading stamps from %s...\n", stampsName);
+                psVector *xStamp = NULL, *yStamp = NULL, *fluxStamp = NULL; // Stamp positions and fluxes
+                if (input) {
+                    // We have x, y because the target is provided by the input image
+                    psArray *stampsData = psVectorsReadFromFile(stampsName, "%f %f"); // Stamp positions
+                    if (!stampsData) {
+                        psError(PS_ERR_IO, false, "Unable to read stamps file %s", stampsName);
+                        goto ERROR;
+                    }
+                    xStamp = psMemIncrRefCounter(stampsData->data[0]);
+                    yStamp = psMemIncrRefCounter(stampsData->data[1]);
+                    psFree(stampsData);
+                } else {
+                    // We have x, y and flux in order to generate a target
+                    psArray *stampsData = psVectorsReadFromFile(stampsName, "%f %f %f"); // Stamp positions
+                    if (!stampsData) {
+                        psError(PS_ERR_IO, false, "Unable to read stamps file %s", stampsName);
+                        goto ERROR;
+                    }
+                    xStamp = psMemIncrRefCounter(stampsData->data[0]);
+                    yStamp = psMemIncrRefCounter(stampsData->data[1]);
+                    fluxStamp = psMemIncrRefCounter(stampsData->data[2]);
+                    psFree(stampsData);
+                }
+
+                // Correct for IRAF/FITS (unit-offset) positions to C (zero-offset) positions
+                psBinaryOp(xStamp, xStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
+                psBinaryOp(yStamp, yStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
+
+                stamps = pmSubtractionStampsSet(xStamp, yStamp, fluxStamp, reference->image, subMask,
+                                                region, stampSpacing, input ? 0 : 2 * footprint);
+            }
+
             // 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,
+                if (!getStamps(&stamps, reference, input, subMask, weight, NULL,
                                threshold, stampSpacing, targetWidth, size, footprint)) {
                     goto ERROR;
@@ -308,5 +293,5 @@
                 psTrace("psModules.imcombine", 2, "Iteration %d...\n", k);
 
-                if (!getStamps(&stamps, stampsData, reference, input, subMask, weight, region,
+                if (!getStamps(&stamps, reference, input, subMask, weight, region,
                                threshold, stampSpacing, targetWidth, size, footprint)) {
                     goto ERROR;
@@ -457,6 +442,4 @@
     psFree(subMask);
     subMask = NULL;
-    psFree(stampsData);
-    stampsData = NULL;
 
     if (!pmSubtractionBorder(convolved->image, convolved->weight, convolved->mask, size, maskBlank)) {
@@ -477,5 +460,4 @@
     psFree(stamps);
     psFree(solution);
-    psFree(stampsData);
     psFree(stamps);
     return false;
