Index: trunk/pois/src/pois.c
===================================================================
--- trunk/pois/src/pois.c	(revision 3810)
+++ trunk/pois/src/pois.c	(revision 3813)
@@ -114,8 +114,10 @@
 
     // Iterate for a good solution
-    bool badStamps = true;		// Do we have bad stamps, such that we need to continue to iterate?
+    psList *badStamps = NULL;		// Do we have bad stamps, such that we need to continue to iterate?
     for (int iterNum = 0; iterNum < config->numIter && badStamps; iterNum++) {
 	psTrace("pois", 1, "Iteration %d...\n", iterNum);
 
+	psFree(badStamps);		// left over from last iteration
+	
 	// Find stamps
 	stamps = poisFindStamps(stamps, refImage, mask, config);
@@ -175,4 +177,5 @@
     // If there was rejection on the last round, re-solve the equation using only the good stamps
     if (badStamps) {
+	psFree(badStamps);
 	solution = poisSolveEquation(solution, stamps, config);
     }
Index: trunk/pois/src/pois.h
===================================================================
--- trunk/pois/src/pois.h	(revision 3810)
+++ trunk/pois/src/pois.h	(revision 3813)
@@ -173,8 +173,8 @@
 
 // Reject stamps, based on the deviations
-bool poisRejectStamps(psArray *stamps,	// Array of stamps
-		      psImage *mask,	// Mask image
-		      const psVector *deviations, // Vector of deviations for the stamps
-		      const poisConfig *config // Configuration
+psList *restrict poisRejectStamps(psArray *stamps,	// Array of stamps
+				  psImage *mask,	// Mask image
+				  const psVector *deviations, // Vector of deviations for the stamps
+				  const poisConfig *config // Configuration
     );
 
Index: trunk/pois/src/poisFindStamps.c
===================================================================
--- trunk/pois/src/poisFindStamps.c	(revision 3810)
+++ trunk/pois/src/poisFindStamps.c	(revision 3813)
@@ -33,7 +33,7 @@
     if (stamps == NULL) {
 	stamps = psArrayAlloc(nsx * nsy);
-	// Initialise
+					// Initialise; should be done by psArrayAlloc
 	for (int i = 0; i < nsx * nsy; i++) {
-	    stamps->data[i] = poisStampAlloc();
+	    stamps->data[i] = NULL;
 	}
     }
@@ -44,4 +44,7 @@
 	for (int j = 0; j < nsy; j++) {
 	    poisStamp *stamp = stamps->data[num]; // The stamp
+	    if (stamp == NULL) {
+		stamp = stamps->data[num] = poisStampAlloc();
+	    }
 
 	    // Only find a new stamp if we need to
Index: trunk/pois/src/poisRejectStamps.c
===================================================================
--- trunk/pois/src/poisRejectStamps.c	(revision 3810)
+++ trunk/pois/src/poisRejectStamps.c	(revision 3813)
@@ -6,8 +6,8 @@
 #define SQUARE(x) ((x)*(x))
 
-bool poisRejectStamps(psArray *stamps,	// Array of stamps
-		      psImage *mask,	// Mask image
-		      const psVector *deviations, // Vector of deviations for the stamps
-		      const poisConfig *config // Configuration
+psList *restrict poisRejectStamps(psArray *stamps, // Array of stamps
+				  psImage *mask,	// Mask image
+				  const psVector *deviations, // Vector of deviations for the stamps
+				  const poisConfig *config // Configuration
     )
 {
@@ -21,5 +21,4 @@
     assert(deviations->type.type == PS_TYPE_F32);
 
-    bool masked = false;		// Have we masked any stamps?
     psVector *devMask = psVectorAlloc(stamps->n, PS_TYPE_U8); // Mask for statistics
 
@@ -30,5 +29,5 @@
 	poisStamp *stamp = stamps->data[i];	// The stamp
 	// Only interested in the stamps that we're actually using
-	if (stamp->status == POIS_STAMP_USED) {
+	if (stamp != NULL && stamp->status == POIS_STAMP_USED) {
 	    meanDev += SQUARE(deviations->data.F32[i]);
 	    numDev++;
@@ -41,8 +40,12 @@
 
     // Reject stamps
+    psList *rejected = NULL;		// rejected stamps
     for (int s = 0; s < deviations->n; s++) {
 	poisStamp *stamp = stamps->data[s];
+	if (stamp == NULL) {
+	    continue;
+	}
+	
 	if (stamp->status == POIS_STAMP_USED && fabsf(deviations->data.F32[s]) > limit) {
-	    masked = true;
 	    psTrace("pois.rejectStamps", 1, "Rejecting stamp %d (%d,%d): %f\n", s, stamp->x, stamp->y,
 		    deviations->data.F32[s]);
@@ -56,5 +59,13 @@
 	    
 	    // Mark stamp for replacement
-	    stamp->status = POIS_STAMP_RESET;
+	    stamps->data[s] = NULL;
+	    
+	    if (rejected == NULL) {
+		rejected = psListAlloc(stamp);
+	    } else {
+		bool failed = psListAdd(rejected, PS_LIST_TAIL, stamp);
+		assert(!failed);
+	    }
+	    psFree(stamp);
 	} // Bad stamps
     } // Iterating over stamps
@@ -62,4 +73,4 @@
     psFree(devMask);
 
-    return masked;
+    return rejected;
 }
