Index: trunk/pois/src/poisCalculateDeviations.c
===================================================================
--- trunk/pois/src/poisCalculateDeviations.c	(revision 6451)
+++ trunk/pois/src/poisCalculateDeviations.c	(revision 6452)
@@ -40,5 +40,7 @@
     psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN); // Statistics
 
+    // Pre-allocate for optimisation:
     psImage *subStamp = psImageAlloc(2 * xSize, 2 * ySize, PS_TYPE_F32); // Subtraction of stamp
+
     for (int s = 0; s < stamps->n; s++) {
         poisStamp *stamp = stamps->data[s]; // The coordinates of the stamp of interest
@@ -55,16 +57,17 @@
             (void)psBinaryOp(subStamp, subStamp, "*", subStamp);
             (void)psBinaryOp(subStamp, subStamp, "/", inStamp);
+
+            // Trim regions.  Need separate regions for the subtraction and the mask, since the subtraction
+            // isn't a child image (it's the subtraction of two child images, but placed in a pre-allocated
+            // image which isn't a child image), but the mask *is* a child image.  Therefore the regions are
+            // different, since they refer to the region on the parent image (if it exists).
             psRegion stampTrim = { config->xKernel, config->xKernel + 2 * footprint, config->yKernel,
                                    config->yKernel + 2 * footprint };
+            psRegion maskTrim = { x - xSize + config->xKernel, x + xSize - config->xKernel,
+                                  y - ySize + config->yKernel, y + ySize - config->yKernel };
+
             psImage *subStampTrim = psImageSubset(subStamp, stampTrim);
-            psImage *maskStampTrim = psImageSubset(maskStamp, stampTrim);
-            // Copy image to workaround bug 305
-            psImage *tempImage = psImageCopy(NULL, subStampTrim, PS_TYPE_F32);
-            psImage *tempMask = psImageCopy(NULL, maskStampTrim, PS_TYPE_U8);
-
-            (void)psImageStats(stats, tempImage, tempMask, POIS_MASK_BAD | POIS_MASK_NEAR_BAD);
-
-            psFree(tempImage);
-            psFree(tempMask);
+            psImage *maskStampTrim = psImageSubset(maskStamp, maskTrim);
+            (void)psImageStats(stats, subStampTrim, maskStampTrim, POIS_MASK_BAD | POIS_MASK_NEAR_BAD);
 
             deviations->data.F32[s] = sqrtf(stats->sampleMean / 2.0);
Index: trunk/pois/src/poisCalculateEquation.c
===================================================================
--- trunk/pois/src/poisCalculateEquation.c	(revision 6451)
+++ trunk/pois/src/poisCalculateEquation.c	(revision 6452)
@@ -32,6 +32,6 @@
     int numKernelParams = kernelParams->n; // Number of kernel parameters
 
-    psPolynomial2D *poly = psPolynomial2DAlloc(config->spatialOrder + 1, config->spatialOrder + 1,
-                                               PS_POLYNOMIAL_ORD); // Polynomial for evaluation
+    psPolynomial2D *poly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, config->spatialOrder + 1,
+                                               config->spatialOrder + 1); // Polynomial for evaluation
     for (int j = 0; j < config->spatialOrder + 1; j++) {
         for (int i = 0; i < config->spatialOrder + 1; i++) {
Index: trunk/pois/src/poisConvolveImage.c
===================================================================
--- trunk/pois/src/poisConvolveImage.c	(revision 6451)
+++ trunk/pois/src/poisConvolveImage.c	(revision 6452)
@@ -30,6 +30,6 @@
     psImage *convolved = psImageAlloc(nx, ny, PS_TYPE_F32); // The convolved image, to be returned
 
-    psPolynomial2D *poly = psPolynomial2DAlloc(config->spatialOrder + 1, config->spatialOrder + 1,
-                                               PS_POLYNOMIAL_ORD ); // Polynomial
+    psPolynomial2D *poly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, config->spatialOrder + 1,
+                                               config->spatialOrder + 1); // Polynomial
     for (int i = 0; i < config->spatialOrder + 1; i++) {
         for (int j = 0; j < config->spatialOrder + 1; j++) {
Index: trunk/pois/src/poisExtractKernel.c
===================================================================
--- trunk/pois/src/poisExtractKernel.c	(revision 6451)
+++ trunk/pois/src/poisExtractKernel.c	(revision 6452)
@@ -6,8 +6,8 @@
 /* Create a kernel image from the solution vector */
 psImage *poisExtractKernel(const psVector *solution, // Vector containing the kernel elements
-			   const psArray *kernelParams, // The kernel parameters
-			   float x,	// The relative (-1 to 1) x position for which to get the kernel
-			   float y,	// The relative (-1 to 1) y position for which to get the kernel
-			   const poisConfig *config // Configuration
+                           const psArray *kernelParams, // The kernel parameters
+                           float x,     // The relative (-1 to 1) x position for which to get the kernel
+                           float y,     // The relative (-1 to 1) y position for which to get the kernel
+                           const poisConfig *config // Configuration
     )
 {
@@ -18,23 +18,23 @@
     assert(y >= -1.0 && y <= 1.0);
 
-    int xKernel = config->xKernel;	// Half-size of kernel in x
-    int yKernel = config->yKernel;	// Half-size of kernel in y
-    float kernelSum = 0.0;		// Sum of the kernel
+    int xKernel = config->xKernel;      // Half-size of kernel in x
+    int yKernel = config->yKernel;      // Half-size of kernel in y
+    float kernelSum = 0.0;              // Sum of the kernel
 
     /* Allocate the image */
     psImage *image = psImageAlloc(2*xKernel + 1, 2*yKernel + 1, PS_TYPE_F32); // The kernel image
     for (int j = 0; j < (2*yKernel + 1); j++) {
-	for (int i = 0; i < (2*xKernel + 1); i++) {
-	    image->data.F32[j][i] = 0.0;
-	}
+        for (int i = 0; i < (2*xKernel + 1); i++) {
+            image->data.F32[j][i] = 0.0;
+        }
     }
 
-    psPolynomial2D *poly = psPolynomial2DAlloc(config->spatialOrder + 1, config->spatialOrder + 1,
-					       PS_POLYNOMIAL_ORD); // Polynomial for evaluation
+    psPolynomial2D *poly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, config->spatialOrder + 1,
+                                               config->spatialOrder + 1); // Polynomial for evaluation
     for (int j = 0; j < config->spatialOrder + 1; j++) {
-	for (int i = 0; i < config->spatialOrder + 1; i++) {
-	    poly->coeff[j][i] = 1.0;
-	    poly->mask[j][i] = 1;	// Mask all coefficients; unmask to evaluate
-	}
+        for (int i = 0; i < config->spatialOrder + 1; i++) {
+            poly->coeff[j][i] = 1.0;
+            poly->mask[j][i] = 1;       // Mask all coefficients; unmask to evaluate
+        }
     }
 
@@ -42,18 +42,18 @@
     /* Iterate over the kernel parameters */
     for (int k = 0; k < kernelParams->n; k++) {
-	poisKernelBasis *kernel = kernelParams->data[k]; // The kernel basis function
+        poisKernelBasis *kernel = kernelParams->data[k]; // The kernel basis function
 
-	int u = kernel->u;
-	int v = kernel->v;
-	int xOrder = kernel->xOrder;
-	int yOrder = kernel->yOrder;
+        int u = kernel->u;
+        int v = kernel->v;
+        int xOrder = kernel->xOrder;
+        int yOrder = kernel->yOrder;
 
-	// Evaluate polynomial
-	poly->mask[xOrder][yOrder] = 0;
-	double evaluation = solution->data.F64[k] * psPolynomial2DEval(poly, x, y);
-	poly->mask[xOrder][yOrder] = 1;
+        // Evaluate polynomial
+        poly->mask[xOrder][yOrder] = 0;
+        double evaluation = solution->data.F64[k] * psPolynomial2DEval(poly, x, y);
+        poly->mask[xOrder][yOrder] = 1;
 
-	image->data.F32[v + yKernel][u + xKernel] += (float)evaluation;
-	kernelSum += (float)evaluation;
+        image->data.F32[v + yKernel][u + xKernel] += (float)evaluation;
+        kernelSum += (float)evaluation;
     }
 
