Index: trunk/psModules/src/imcombine/pmSubtraction.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtraction.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtraction.c	(revision 27086)
@@ -17,4 +17,5 @@
 #include <pslib.h>
 
+#include "pmErrorCodes.h"
 #include "pmHDU.h"                      // Required for pmFPA.h
 #include "pmFPA.h"
@@ -752,5 +753,5 @@
 
     if (stamp->status != PM_SUBTRACTION_STAMP_CALCULATE) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Stamp not marked for calculation.");
+        psError(PM_ERR_PROG, true, "Stamp not marked for calculation.");
         return false;
     }
@@ -808,5 +809,5 @@
 
     if (numStamps == 0) {
-        psError(PS_ERR_UNKNOWN, true, "No good stamps found.");
+        psError(PM_ERR_STAMPS, true, "No good stamps found.");
         psFree(mask);
         return -1;
@@ -816,5 +817,5 @@
                                   PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_QUARTILE); // Statistics for deviatns
     if (!psVectorStats(stats, deviations, NULL, mask, 0xff)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to measure statistics for deviations.");
+        psError(PM_ERR_DATA, false, "Unable to measure statistics for deviations.");
         psFree(stats);
         psFree(mask);
@@ -1199,5 +1200,5 @@
     if (region && psRegionIsNaN(*region)) {
         psString string = psRegionToString(*region);
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Input region (%s) contains NAN values", string);
+        psError(PM_ERR_PROG, true, "Input region (%s) contains NAN values", string);
         psFree(string);
         return false;
@@ -1303,5 +1304,5 @@
 
     if (!psThreadPoolWait(false)) {
-        psError(PS_ERR_UNKNOWN, false, "Error waiting for threads.");
+        psError(psErrorCodeLast(), false, "Error waiting for threads.");
         return false;
     }
Index: trunk/psModules/src/imcombine/pmSubtractionAnalysis.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionAnalysis.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionAnalysis.c	(revision 27086)
@@ -74,5 +74,5 @@
                                                            false); // Image of the kernel
                 if (!kernel) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to generate kernel image.");
+                    psError(psErrorCodeLast(), false, "Unable to generate kernel image.");
                     psFree(convKernels);
                     return false;
@@ -81,5 +81,5 @@
                 if (psImageOverlaySection(convKernels, kernel, (i + KERNEL_MOSAIC) * fullSize,
                                           (j + KERNEL_MOSAIC) * fullSize, "=") == 0) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to overlay kernel image.");
+                    psError(psErrorCodeLast(), false, "Unable to overlay kernel image.");
                     psFree(kernel);
                     psFree(convKernels);
@@ -93,5 +93,5 @@
                                                       true); // Image of the kernel
                     if (!kernel) {
-                        psError(PS_ERR_UNKNOWN, false, "Unable to generate kernel image.");
+                        psError(psErrorCodeLast(), false, "Unable to generate kernel image.");
                         psFree(convKernels);
                         return false;
@@ -101,5 +101,5 @@
                                               (2 * KERNEL_MOSAIC + 1 + i + KERNEL_MOSAIC) * fullSize + 4,
                                               (j + KERNEL_MOSAIC) * fullSize, "=") == 0) {
-                        psError(PS_ERR_UNKNOWN, false, "Unable to overlay kernel image.");
+                        psError(psErrorCodeLast(), false, "Unable to overlay kernel image.");
                         psFree(kernel);
                         psFree(convKernels);
@@ -161,5 +161,5 @@
         psImage *image = pmSubtractionKernelImage(kernels, 0.5, 0.5, false); // Image of the kernel
         if (!image) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to generate image of kernel.");
+            psError(psErrorCodeLast(), false, "Unable to generate image of kernel.");
             return false;
         }
@@ -216,5 +216,5 @@
         psImage *image = pmSubtractionKernelImage(kernels, 0.5, 0.5, false); // Image of the kernel
         if (!image) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to generate image of kernel.");
+            psError(psErrorCodeLast(), false, "Unable to generate image of kernel.");
             return false;
         }
Index: trunk/psModules/src/imcombine/pmSubtractionEquation.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionEquation.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionEquation.c	(revision 27086)
@@ -7,4 +7,5 @@
 #include <pslib.h>
 
+#include "pmErrorCodes.h"
 #include "pmSubtraction.h"
 #include "pmSubtractionKernels.h"
@@ -718,5 +719,5 @@
     // Generate convolutions: these are generated once and saved
     if (!pmSubtractionConvolveStamp(stamp, kernels, footprint)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to convolve stamp %d.", index);
+        psError(psErrorCodeLast(), false, "Unable to convolve stamp %d.", index);
         return NULL;
     }
@@ -869,5 +870,5 @@
 
     if (!psThreadPoolWait(true)) {
-        psError(PS_ERR_UNKNOWN, false, "Error waiting for threads.");
+        psError(psErrorCodeLast(), false, "Error waiting for threads.");
         return false;
     }
@@ -989,5 +990,5 @@
             psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics for norm
             if (!psVectorStats(stats, norms, NULL, NULL, 0)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to determine median normalisation");
+                psError(PM_ERR_DATA, false, "Unable to determine median normalisation");
                 psFree(stats);
                 psFree(sumMatrix);
@@ -1134,5 +1135,5 @@
             psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics for norm
             if (!psVectorStats(stats, norms, NULL, NULL, 0)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to determine median normalisation");
+                psError(PM_ERR_DATA, false, "Unable to determine median normalisation");
                 psFree(stats);
                 psFree(sumMatrix);
@@ -1371,5 +1372,5 @@
                                                           false); // Kernel image
                 if (!image) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to generate image of kernel.");
+                    psError(psErrorCodeLast(), false, "Unable to generate image of kernel.");
                     return false;
                 }
@@ -1947,5 +1948,5 @@
             psVector *w = NULL;
             if (!psMatrixSVD (&U, &w, &V, kernelMatrix)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to perform SVD on sumMatrix\n");
+                psError(psErrorCodeLast(), false, "failed to perform SVD on sumMatrix\n");
                 return NULL;
             }
@@ -2087,5 +2088,5 @@
             psImage *luMatrix = psMatrixLUDecomposition(NULL, &permutation, sumMatrix);
             if (!luMatrix) {
-                psError(PS_ERR_UNKNOWN, true, "LU Decomposition of least-squares matrix failed.\n");
+                psError(PM_ERR_DATA, true, "LU Decomposition of least-squares matrix failed.\n");
                 psFree(solution);
                 psFree(sumVector);
@@ -2100,5 +2101,5 @@
             psFree(permutation);
             if (!solution) {
-                psError(PS_ERR_UNKNOWN, true, "Failed to solve the least-squares system.\n");
+                psError(PM_ERR_DATA, true, "Failed to solve the least-squares system.\n");
                 psFree(solution);
                 psFree(sumVector);
Index: trunk/psModules/src/imcombine/pmSubtractionIO.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionIO.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionIO.c	(revision 27086)
@@ -3,4 +3,5 @@
 #include <string.h>
 
+#include "pmErrorCodes.h"
 #include "pmHDU.h"
 #include "pmHDUUtils.h"
@@ -66,5 +67,5 @@
     }
     if (regions->n == 0) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "No subtraction regions found.");
+        psError(PM_ERR_PROG, true, "No subtraction regions found.");
         psFree(regions);
         return false;
@@ -87,5 +88,5 @@
 
     if (regions->n != kernels->n) {
-        psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Number of regions (%ld) and kernels (%ld) don't match.\n",
+        psError(PM_ERR_PROG, true, "Number of regions (%ld) and kernels (%ld) don't match.\n",
                 regions->n, kernels->n);
         psFree(regions);
@@ -165,5 +166,5 @@
 
     if (!psFitsWriteTable(fits, header, rows, EXTNAME_KERNEL)) {
-        psError(PS_ERR_IO, false, "Unable to write subtraction kernel to FITS table.");
+        psError(psErrorCodeLast(), false, "Unable to write subtraction kernel to FITS table.");
         psFree(header);
         psFree(rows);
@@ -175,5 +176,5 @@
 
     if (image && !psFitsWriteImage(fits, header, image, 0, EXTNAME_IMAGE)) {
-        psError(PS_ERR_IO, false, "Unable to write subtraction kernel image.");
+        psError(psErrorCodeLast(), false, "Unable to write subtraction kernel image.");
         psFree(header);
         psFree(rows);
@@ -201,5 +202,5 @@
         thisView->readout = i;
         if (!pmReadoutWriteSubtractionKernels(readout, file->fits)) {
-            psError(PS_ERR_IO, false, "Failed to write %dth readout", i);
+            psError(psErrorCodeLast(), false, "Failed to write %dth readout", i);
             psFree(thisView);
             return false;
@@ -224,5 +225,5 @@
         thisView->cell = i;
         if (!pmCellWriteSubtractionKernels(cell, thisView, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write %dth cell", i);
+            psError(psErrorCodeLast(), false, "Failed to write %dth cell", i);
             psFree(thisView);
             return false;
@@ -247,5 +248,5 @@
         thisView->chip = i;
         if (!pmChipWriteSubtractionKernels(chip, thisView, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write %dth chip", i);
+            psError(psErrorCodeLast(), false, "Failed to write %dth chip", i);
             psFree(thisView);
             return false;
@@ -265,5 +266,5 @@
 
     if (!psFitsMoveExtName(fits, EXTNAME_KERNEL)) {
-        psError(PS_ERR_IO, false, "Unable to move to subtraction kernel table.");
+        psError(psErrorCodeLast(), false, "Unable to move to subtraction kernel table.");
         return false;
     }
@@ -271,5 +272,5 @@
     psArray *table = psFitsReadTable(fits); // Table of interest
     if (!table) {
-        psError(PS_ERR_IO, false, "Unable to read FITS table");
+        psError(psErrorCodeLast(), false, "Unable to read FITS table");
         return false;
     }
@@ -282,5 +283,5 @@
         TARGET = psMetadataLookup##SUFFIX(&mdok, row, NAME); \
         if (!mdok) { \
-            psError(PS_ERR_UNKNOWN, false, "Unable to find column %s in subtraction kernel table.", NAME); \
+            psError(PM_ERR_PROG, false, "Unable to find column %s in subtraction kernel table.", NAME); \
             psFree(table); \
             return false; \
@@ -325,5 +326,5 @@
             kernels->solution1 = psMemIncrRefCounter(psMetadataLookupPtr(&mdok, row, NAME_SOL1));
             if (!mdok) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to find column %s in subtraction kernel table.",
+                psError(PM_ERR_PROG, false, "Unable to find column %s in subtraction kernel table.",
                         NAME_SOL1);
                 psFree(kernels);
@@ -335,5 +336,5 @@
             kernels->solution1 = psMemIncrRefCounter(psMetadataLookupPtr(&mdok, row, NAME_SOL1));
             if (!mdok) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to find column %s in subtraction kernel table.",
+                psError(PM_ERR_PROG, false, "Unable to find column %s in subtraction kernel table.",
                         NAME_SOL1);
                 psFree(kernels);
@@ -343,5 +344,5 @@
             kernels->solution2 = psMemIncrRefCounter(psMetadataLookupPtr(&mdok, row, NAME_SOL2));
             if (!mdok) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to find column %s in subtraction kernel table.",
+                psError(PM_ERR_PROG, false, "Unable to find column %s in subtraction kernel table.",
                         NAME_SOL2);
                 psFree(kernels);
@@ -379,5 +380,9 @@
         pmReadout *readout = cell->readouts->data[i];
         thisView->readout = i;
-        pmReadoutReadSubtractionKernels(readout, file->fits);
+        if (!pmReadoutReadSubtractionKernels(readout, file->fits)) {
+            psError(psErrorCodeLast(), false, "Unable to read subtraction kernels from cell");
+            psFree(thisView);
+            return false;
+        }
         if (!readout->data_exists) {
             continue;
@@ -410,6 +415,10 @@
         pmCell *cell = chip->cells->data[i];
         thisView->cell = i;
-        pmCellReadSubtractionKernels(cell, thisView, file, config);
-        if (!cell->data_exists) {
+        if (!pmCellReadSubtractionKernels(cell, thisView, file, config)) {
+            psError(psErrorCodeLast(), false, "Unable to read subtraction kernels from cell");
+            psFree(thisView);
+            return false;
+        }
+         if (!cell->data_exists) {
             continue;
         }
@@ -419,5 +428,5 @@
 
     if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
-        psError(PS_ERR_IO, false, "Failed to read concepts for chip.\n");
+        psError(psErrorCodeLast(), false, "Failed to read concepts for chip.\n");
         return false;
     }
@@ -440,10 +449,14 @@
         pmChip *chip = fpa->chips->data[i];
         thisView->chip = i;
-        pmChipReadSubtractionKernels(chip, thisView, file, config);
+        if (!pmChipReadSubtractionKernels(chip, thisView, file, config)) {
+            psError(psErrorCodeLast(), false, "Unable to read subtraction kernels from chip");
+            psFree(thisView);
+            return false;
+        }
     }
     psFree(thisView);
 
     if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
-        psError(PS_ERR_IO, false, "Failed to read concepts for fpa.\n");
+        psError(psErrorCodeLast(), false, "Failed to read concepts for fpa.\n");
         return false;
     }
@@ -464,5 +477,5 @@
     if (view->chip == -1) {
         if (!pmFPAWriteSubtractionKernels(fpa, view, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write subtraction kernels from fpa");
+            psError(psErrorCodeLast(), false, "Failed to write subtraction kernels from fpa");
             psFree(fpa);
             return false;
@@ -473,5 +486,5 @@
 
     if (view->chip >= fpa->chips->n) {
-        psError(PS_ERR_UNKNOWN, false, "Writing chip == %d (>= chips->n == %ld)", view->chip, fpa->chips->n);
+        psError(PM_ERR_PROG, true, "Writing chip == %d (>= chips->n == %ld)", view->chip, fpa->chips->n);
         psFree(fpa);
         return false;
@@ -481,5 +494,5 @@
     if (view->cell == -1) {
         if (!pmChipWriteSubtractionKernels(chip, view, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write objects from chip");
+            psError(psErrorCodeLast(), false, "Failed to write objects from chip");
             psFree(fpa);
             return false;
@@ -490,5 +503,5 @@
 
     if (view->cell >= chip->cells->n) {
-        psError(PS_ERR_UNKNOWN, false, "Writing cell == %d (>= cells->n == %ld)",
+        psError(PM_ERR_PROG, true, "Writing cell == %d (>= cells->n == %ld)",
                 view->cell, chip->cells->n);
         psFree(fpa);
@@ -499,5 +512,5 @@
     if (view->readout == -1) {
         if (!pmCellWriteSubtractionKernels(cell, view, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write objects from cell");
+            psError(psErrorCodeLast(), false, "Failed to write objects from cell");
             psFree(fpa);
             return false;
@@ -508,5 +521,5 @@
 
     if (view->readout >= cell->readouts->n) {
-        psError(PS_ERR_UNKNOWN, false, "Writing readout == %d (>= readouts->n == %ld)",
+        psError(PM_ERR_PROG, true, "Writing readout == %d (>= readouts->n == %ld)",
                 view->readout, cell->readouts->n);
         psFree(fpa);
@@ -516,5 +529,5 @@
 
     if (!pmReadoutWriteSubtractionKernels(readout, file->fits)) {
-        psError(PS_ERR_IO, false, "Failed to write objects from readout %d", view->readout);
+        psError(psErrorCodeLast(), false, "Failed to write objects from readout %d", view->readout);
         psFree(fpa);
         return false;
Index: trunk/psModules/src/imcombine/pmSubtractionMask.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMask.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionMask.c	(revision 27086)
@@ -146,5 +146,5 @@
     if (!psImageConvolveMask(mask, mask, PM_SUBTRACTION_MASK_BAD_1, PM_SUBTRACTION_MASK_CONVOLVE_1,
                              -size, size, -size, size)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to convolve bad pixels from mask 1.");
+        psError(psErrorCodeLast(), false, "Unable to convolve bad pixels from mask 1.");
         psFree(mask);
         return NULL;
@@ -152,5 +152,5 @@
     if (!psImageConvolveMask(mask, mask, PM_SUBTRACTION_MASK_BAD_2, PM_SUBTRACTION_MASK_CONVOLVE_2,
                              -size, size, -size, size)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to convolve bad pixels from mask 2.");
+        psError(psErrorCodeLast(), false, "Unable to convolve bad pixels from mask 2.");
         psFree(mask);
         return NULL;
@@ -176,5 +176,5 @@
     if (!psImageConvolveMask(mask, mask, maskRej, PM_SUBTRACTION_MASK_REJ,
                              -footprint, footprint, -footprint, footprint)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to convolve bad pixels.");
+        psError(psErrorCodeLast(), false, "Unable to convolve bad pixels.");
         psFree(mask);
         return NULL;
Index: trunk/psModules/src/imcombine/pmSubtractionMatch.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 27086)
@@ -104,5 +104,5 @@
     psTrace("psModules.imcombine", 3, "Extracting stamps...\n");
     if (!pmSubtractionStampsExtract(*stamps, ro1->image, ro2->image, variance, size, *region)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to extract stamps.");
+        psError(psErrorCodeLast(), false, "Unable to extract stamps.");
         return false;
     }
@@ -318,5 +318,5 @@
         while ((item = psMetadataGetAndIncrement(iter))) {
             if (item->type != PS_DATA_UNKNOWN) {
-                psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unexpected type for kernel.");
+                psError(PM_ERR_PROG, true, "Unexpected type for kernel.");
                 psFree(iter);
                 psFree(kernelList);
@@ -338,5 +338,5 @@
     }
     if (psListLength(kernelList) == 0) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to find kernels");
+        psError(PM_ERR_PROG, true, "Unable to find kernels");
         psFree(kernelList);
         return false;
@@ -353,5 +353,5 @@
         while ((item = psMetadataGetAndIncrement(iter))) {
             if (item->type != PS_DATA_REGION) {
-                psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unexpected type for region.");
+                psError(PM_ERR_PROG, true, "Unexpected type for region.");
                 psFree(iter);
                 psFree(kernels);
@@ -365,5 +365,5 @@
     }
     if (regions->n != kernels->n) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Differing number of kernels (%ld) and regions (%ld)",
+        psError(PM_ERR_PROG, true, "Differing number of kernels (%ld) and regions (%ld)",
                 kernels->n, regions->n);
         psFree(regions);
@@ -406,5 +406,5 @@
         if (!pmSubtractionAnalysis(outAnalysis, outHeader, kernel, region,
                                    ro1->image->numCols, ro1->image->numRows)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to generate QA data");
+            psError(psErrorCodeLast(), false, "Unable to generate QA data");
             psFree(outAnalysis);
             psFree(outHeader);
@@ -417,5 +417,5 @@
         if (!pmSubtractionConvolve(conv1, conv2, ro1, ro2, subMask, stride, maskBad, maskPoor, poorFrac,
                                    kernelError, covarFrac, region, kernel, true, useFFT)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to convolve image.");
+            psError(psErrorCodeLast(), false, "Unable to convolve image.");
             psFree(outAnalysis);
             psFree(outHeader);
@@ -555,5 +555,5 @@
             psStatsInit(bg);
             if (!psImageBackground(bg, NULL, ro1->image, ro1->mask, maskVal, rng)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics.");
+                psError(PM_ERR_DATA, false, "Unable to measure background statistics.");
                 psFree(bg);
                 goto MATCH_ERROR;
@@ -565,5 +565,5 @@
             psStatsInit(bg);
             if (!psImageBackground(bg, NULL, ro2->image, ro2->mask, maskVal, rng)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics.");
+                psError(PM_ERR_DATA, false, "Unable to measure background statistics.");
                 psFree(bg);
                 goto MATCH_ERROR;
@@ -640,5 +640,5 @@
                 psVector *buffer = NULL;// Buffer for stats
                 if (!psImageBackground(bgStats, &buffer, ro1->image, ro1->mask, maskVal, rng)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to measure background of image 1.");
+                    psError(PM_ERR_DATA, false, "Unable to measure background of image 1.");
                     psFree(bgStats);
                     psFree(buffer);
@@ -647,5 +647,5 @@
                 float bg1 = psStatsGetValue(bgStats, BG_STAT); // Background for image 1
                 if (!psImageBackground(bgStats, &buffer, ro2->image, ro2->mask, maskVal, rng)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to measure background of image 2.");
+                    psError(PM_ERR_DATA, false, "Unable to measure background of image 2.");
                     psFree(bgStats);
                     psFree(buffer);
@@ -667,5 +667,5 @@
                     break;
                   default:
-                    psError(PS_ERR_UNKNOWN, false, "Unable to determine subtraction order.");
+                    psError(psErrorCodeLast(), false, "Unable to determine subtraction order.");
                     goto MATCH_ERROR;
                 }
@@ -685,5 +685,5 @@
                 // generate the window function from the set of stamps
                 if (!pmSubtractionStampsGetWindow(stamps, size)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to get stamps window.");
+                    psError(psErrorCodeLast(), false, "Unable to get stamps window.");
                     goto MATCH_ERROR;
                 }
@@ -692,5 +692,5 @@
                 psTrace("psModules.imcombine", 3, "Calculating equation for normalization...\n");
                 if (!pmSubtractionCalculateEquation(stamps, kernels, SUBMODE)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -698,5 +698,5 @@
                 psTrace("psModules.imcombine", 3, "Solving equation for normalization...\n");
                 if (!pmSubtractionSolveEquation(kernels, stamps, SUBMODE)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -710,5 +710,5 @@
                 psTrace("psModules.imcombine", 3, "Calculating equation for kernels...\n");
                 if (!pmSubtractionCalculateEquation(stamps, kernels, PM_SUBTRACTION_EQUATION_KERNELS)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -717,5 +717,5 @@
                 psTrace("psModules.imcombine", 3, "Solving equation for kernels...\n");
                 if (!pmSubtractionSolveEquation(kernels, stamps, PM_SUBTRACTION_EQUATION_KERNELS)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -724,5 +724,5 @@
                 psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
                 if (!deviations) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate deviations.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
                     goto MATCH_ERROR;
                 }
@@ -733,5 +733,5 @@
                 numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej);
                 if (numRejected < 0) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps.");
+                    psError(psErrorCodeLast(), false, "Unable to reject stamps.");
                     psFree(deviations);
                     goto MATCH_ERROR;
@@ -747,5 +747,5 @@
                 psTrace("psModules.imcombine", 3, "Calculating equation for normalization...\n");
                 if (!pmSubtractionCalculateEquation(stamps, kernels, SUBMODE)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -754,5 +754,5 @@
                 psTrace("psModules.imcombine", 3, "Solving equation for kernels...\n");
                 if (!pmSubtractionSolveEquation(kernels, stamps, SUBMODE)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -765,5 +765,5 @@
                 psTrace("psModules.imcombine", 3, "Calculating equation for normalization...\n");
                 if (!pmSubtractionCalculateEquation(stamps, kernels, PM_SUBTRACTION_EQUATION_KERNELS)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -772,5 +772,5 @@
                 psTrace("psModules.imcombine", 3, "Solving equation for kernels...\n");
                 if (!pmSubtractionSolveEquation(kernels, stamps, PM_SUBTRACTION_EQUATION_KERNELS)) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
                     goto MATCH_ERROR;
                 }
@@ -779,5 +779,5 @@
                 psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
                 if (!deviations) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate deviations.");
+                    psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
                     goto MATCH_ERROR;
                 }
@@ -791,5 +791,5 @@
 
             if (!pmSubtractionAnalysis(analysis, header, kernels, region, numCols, numRows)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to generate QA data");
+                psError(psErrorCodeLast(), false, "Unable to generate QA data");
                 goto MATCH_ERROR;
             }
@@ -800,5 +800,5 @@
             if (!pmSubtractionConvolve(conv1, conv2, ro1, ro2, subMask, stride, maskBad, maskPoor, poorFrac,
                                        kernelError, covarFrac, region, kernels, true, useFFT)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to convolve image.");
+                psError(psErrorCodeLast(), false, "Unable to convolve image.");
                 goto MATCH_ERROR;
             }
@@ -820,9 +820,9 @@
 
     if (conv1 && !pmSubtractionBorder(conv1->image, conv1->variance, conv1->mask, size, maskBad)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to set border of convolved image.");
+        psError(psErrorCodeLast(), false, "Unable to set border of convolved image.");
         goto MATCH_ERROR;
     }
     if (conv2 && !pmSubtractionBorder(conv2->image, conv2->variance, conv2->mask, size, maskBad)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to set border of convolved image.");
+        psError(psErrorCodeLast(), false, "Unable to set border of convolved image.");
         goto MATCH_ERROR;
     }
@@ -1023,5 +1023,5 @@
         } else {
             if (!pmSubtractionOrderStamp(ratios, mask, stamps, models, modelSums, i, bg1, bg2)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to measure PSF width for stamp %d", i);
+                psError(psErrorCodeLast(), false, "Unable to measure PSF width for stamp %d", i);
                 psFree(models);
                 psFree(modelSums);
@@ -1034,5 +1034,5 @@
 
     if (!psThreadPoolWait(true)) {
-        psError(PS_ERR_UNKNOWN, false, "Error waiting for threads.");
+        psError(psErrorCodeLast(), false, "Error waiting for threads.");
         psFree(models);
         psFree(modelSums);
@@ -1047,5 +1047,5 @@
     psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN);
     if (!psVectorStats(stats, ratios, NULL, mask, 0xff)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to calculate statistics for moments ratio.");
+        psError(psErrorCodeLast(), false, "Unable to calculate statistics for moments ratio.");
         psFree(mask);
         psFree(ratios);
@@ -1083,5 +1083,5 @@
     psTrace("psModules.imcombine", 3, "Calculating %s normalization equation...\n", description);
     if (!pmSubtractionCalculateEquation(stamps, kernels, SUBMODE)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
         return false;
     }
@@ -1089,5 +1089,5 @@
     psTrace("psModules.imcombine", 3, "Solving %s normalization equation...\n", description);
     if (!pmSubtractionSolveEquation(kernels, stamps, SUBMODE)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
         return false;
     }
@@ -1099,5 +1099,5 @@
     psTrace("psModules.imcombine", 3, "Calculating %s kernel coeffs equation...\n", description);
     if (!pmSubtractionCalculateEquation(stamps, kernels, PM_SUBTRACTION_EQUATION_KERNELS)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
         return false;
     }
@@ -1105,5 +1105,5 @@
     psTrace("psModules.imcombine", 3, "Solving %s kernel coeffs equation...\n", description);
     if (!pmSubtractionSolveEquation(kernels, stamps, PM_SUBTRACTION_EQUATION_KERNELS)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
         return false;
     }
@@ -1113,5 +1113,5 @@
     psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
     if (!deviations) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to calculate deviations.");
+        psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
         return false;
     }
@@ -1120,5 +1120,5 @@
     long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej);
     if (numRejected < 0) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps.");
+        psError(psErrorCodeLast(), false, "Unable to reject stamps.");
         psFree(deviations);
         return false;
@@ -1130,5 +1130,5 @@
         psTrace("psModules.imcombine", 3, "Calculating %s normalization equation...\n", description);
         if (!pmSubtractionCalculateEquation(stamps, kernels, PM_SUBTRACTION_EQUATION_ALL)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+            psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
             return false;
         }
@@ -1136,5 +1136,5 @@
         psTrace("psModules.imcombine", 3, "Resolving %s equation...\n", description);
         if (!pmSubtractionSolveEquation(kernels, stamps, PM_SUBTRACTION_EQUATION_ALL)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+            psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
             return false;
         }
@@ -1147,5 +1147,5 @@
         psTrace("psModules.imcombine", 3, "Calculating %s normalization equation...\n", description);
         if (!pmSubtractionCalculateEquation(stamps, kernels, PM_SUBTRACTION_EQUATION_KERNELS)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+            psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
             return false;
         }
@@ -1153,5 +1153,5 @@
         psTrace("psModules.imcombine", 3, "Resolving %s equation...\n", description);
         if (!pmSubtractionSolveEquation(kernels, stamps, PM_SUBTRACTION_EQUATION_KERNELS)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
+            psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
             return false;
         }
@@ -1161,5 +1161,5 @@
         psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
         if (!deviations) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to calculate deviations.");
+            psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
             return false;
         }
@@ -1167,5 +1167,5 @@
         long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN);
         if (numRejected < 0) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps.");
+            psError(psErrorCodeLast(), false, "Unable to reject stamps.");
             psFree(deviations);
             return false;
@@ -1191,5 +1191,5 @@
 
     if (!subtractionModeTest(stamps1, kernels1, "convolve 1", subMask1, rej)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to test subtraction with convolution of image 1");
+        psError(psErrorCodeLast(), false, "Unable to test subtraction with convolution of image 1");
         psFree(stamps1);
         psFree(kernels1);
@@ -1206,5 +1206,5 @@
 
     if (!subtractionModeTest(stamps2, kernels2, "convolve 2", subMask2, rej)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to test subtraction with convolution of image 2");
+        psError(psErrorCodeLast(), false, "Unable to test subtraction with convolution of image 2");
         psFree(stamps2);
         psFree(kernels2);
Index: trunk/psModules/src/imcombine/pmSubtractionParams.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionParams.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionParams.c	(revision 27086)
@@ -8,4 +8,5 @@
 #include <pslib.h>
 
+#include "pmErrorCodes.h"
 #include "pmSubtractionStamps.h"
 #include "pmSubtraction.h"
@@ -281,5 +282,5 @@
         }
         if (!pmSubtractionConvolveStamp(stamp, kernels, footprint)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to convolve stamp %d.", i);
+            psError(psErrorCodeLast(), false, "Unable to convolve stamp %d.", i);
             psFree(targets);
             psFree(kernels);
@@ -297,5 +298,5 @@
         }
         if (!isfinite(sum1)) {
-            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+            psError(PM_ERR_DATA, true,
                     "Sum of 1/sigma^2 is non-finite for stamp %d (%d,%d)\n",
                     i, (int)stamp->x, (int)stamp->y);
@@ -369,5 +370,5 @@
 
         if (bestIndex == -1) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to find best kernel component in round %d.", iter);
+            psError(PM_ERR_DATA, true, "Unable to find best kernel component in round %d.", iter);
             psFree(targets);
             psFree(sumC);
@@ -408,5 +409,5 @@
 
     if (cutIndex < 0) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to converge to tolerance %g\n", tolerance);
+        psError(PM_ERR_DATA, true, "Unable to converge to tolerance %g\n", tolerance);
         psFree(ranking);
         psFree(kernels);
Index: trunk/psModules/src/imcombine/pmSubtractionStamps.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 27085)
+++ trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 27086)
@@ -369,5 +369,5 @@
         if (psRegionIsNaN(*region)) {
             psString string = psRegionToString(*region);
-            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Input region (%s) contains NAN values", string);
+            psError(PM_ERR_PROG, true, "Input region (%s) contains NAN values", string);
             psFree(string);
             return false;
@@ -376,5 +376,5 @@
             region->y0 < 0 || region->y1 > numRows) {
             psString string = psRegionToString(*region);
-            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Input region (%s) does not fit in image (%dx%d)",
+            psError(PM_ERR_PROG, true, "Input region (%s) does not fit in image (%dx%d)",
                     string, numCols, numRows);
             psFree(string);
@@ -785,5 +785,5 @@
 
         if (x < bounds.x0 + size || x > bounds.x1 - size || y < bounds.y0 + size || y > bounds.y1 - size) {
-            psError(PS_ERR_UNKNOWN, false, "Stamp %d (%d,%d) is within the region border.\n", i, x, y);
+            psError(PM_ERR_PROG, false, "Stamp %d (%d,%d) is within the region border.\n", i, x, y);
             return false;
         }
@@ -881,5 +881,5 @@
 
     if (!stamps) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to set stamps from sources.");
+        psError(psErrorCodeLast(), false, "Unable to set stamps from sources.");
     }
 
@@ -898,5 +898,5 @@
     psArray *data = psVectorsReadFromFile(filename, "%f %f");
     if (!data) {
-        psError(PS_ERR_IO, false, "Unable to read stamps file %s", filename);
+        psError(psErrorCodeLast(), false, "Unable to read stamps file %s", filename);
         return NULL;
     }
