Index: trunk/ppSub/src/ppSub.c
===================================================================
--- trunk/ppSub/src/ppSub.c	(revision 26899)
+++ trunk/ppSub/src/ppSub.c	(revision 26982)
@@ -24,24 +24,10 @@
 int main(int argc, char *argv[])
 {
-
-# if 0
     psLibInit(NULL);
-    pmVisualSetVisual(true);
-    for (int order = 2; order < 11; order ++) {
-	pmSubtractionDeconvolutionTest (order);
-    }
-    psLibFinalize();
-    exit (1);
-# endif
-
-    psExit exitValue = PS_EXIT_SUCCESS; // Exit value
     psTimerStart("ppSub");
-    psLibInit(NULL);
 
     ppSubData *data = NULL;             // Processing data
     pmConfig *config = pmConfigRead(&argc, argv, PPSUB_RECIPE); // Configuration
     if (!config) {
-        psErrorStackPrint(stderr, "Error reading configuration.");
-        exitValue = PS_EXIT_CONFIG_ERROR;
         goto die;
     }
@@ -50,6 +36,5 @@
 
     if (!pmModelClassInit()) {
-        psErrorStackPrint(stderr, "Error initialising model classes.\n");
-        exitValue = PS_EXIT_PROG_ERROR;
+        psError(PPSUB_ERR_PROG, false, "Unable to initialise model classes.");
         psFree(config);
         goto die;
@@ -57,6 +42,5 @@
 
     if (!psphotInit()) {
-        psErrorStackPrint(stderr, "Error initialising psphot.\n");
-        exitValue = PS_EXIT_PROG_ERROR;
+        psError(PPSUB_ERR_PROG, false, "Error initialising psphot.");
         psFree(config);
         goto die;
@@ -66,18 +50,13 @@
 
     if (!ppSubArguments(argc, argv, data)) {
-        psErrorStackPrint(stderr, "Error reading arguments.\n");
-        exitValue = PS_EXIT_CONFIG_ERROR;
+        psError(PPSUB_ERR_ARGUMENTS, false, "Error reading arguments.");
         goto die;
     }
 
     if (!ppSubCamera(data)) {
-        psErrorStackPrint(stderr, "Error setting up camera.\n");
-        exitValue = PS_EXIT_CONFIG_ERROR;
         goto die;
     }
 
     if (!ppSubLoop(data)) {
-        psErrorStackPrint(stderr, "Error performing subtraction.\n");
-        exitValue = PS_EXIT_SYS_ERROR;
         goto die;
     }
@@ -87,12 +66,13 @@
     psTimerStop();
 
-    psString dump_file = psMetadataLookupStr(NULL, data->config->arguments, "-dumpconfig");
-    if (dump_file) {
-        if (!pmConfigDump(data->config, dump_file)) {
-            psErrorStackPrint(stderr, "Unable to dump configuration.\n");
-            exitValue = PS_EXIT_SYS_ERROR;
+    if (data) {
+        psString dump_file = psMetadataLookupStr(NULL, data->config->arguments, "-dumpconfig");
+        if (dump_file) {
+            if (!pmConfigDump(data->config, dump_file)) {
+                psError(PPSUB_ERR_IO, false, "Unable to dump configuration.");
+            }
         }
+        psFree(data);
     }
-    psFree(data);
 
     pmVisualClose(); //close plot windows, if -visual is set
@@ -101,4 +81,44 @@
     psLibFinalize();
 
+    psExit exitValue = PS_EXIT_SUCCESS;        // Exit value for program
+    psErrorCode errorCode = psErrorCodeLast(); // Error code
+    if (errorCode != PS_ERR_NONE) {
+        psErrorStackPrint(stderr, "Unable to perform subtraction.");
+        switch (errorCode) {
+          case PPSUB_ERR_UNKNOWN:
+          case PS_ERR_UNKNOWN:
+            exitValue = PS_EXIT_UNKNOWN_ERROR;
+            break;
+          case PS_ERR_IO:
+          case PS_ERR_DB_CLIENT:
+          case PS_ERR_DB_SERVER:
+          case PS_ERR_BAD_FITS:
+          case PS_ERR_OS_CALL_FAILED:
+          case PPSUB_ERR_IO:
+            exitValue = PS_EXIT_SYS_ERROR;
+            break;
+          case PS_ERR_BAD_PARAMETER_VALUE:
+          case PS_ERR_BAD_PARAMETER_TYPE:
+          case PS_ERR_BAD_PARAMETER_NULL:
+          case PS_ERR_BAD_PARAMETER_SIZE:
+          case PPSUB_ERR_ARGUMENTS:
+          case PPSUB_ERR_CONFIG:
+            exitValue = PS_EXIT_CONFIG_ERROR;
+            break;
+          case PPSUB_ERR_DATA:
+          case PPSUB_ERR_NO_OVERLAP:
+            exitValue = PS_EXIT_DATA_ERROR;
+            break;
+          case PS_ERR_UNEXPECTED_NULL:
+          case PS_ERR_PROGRAMMING:
+          case PPSUB_ERR_NOT_IMPLEMENTED:
+          case PPSUB_ERR_PROG:
+          default:
+            // It's a programming error if we're not dealing with the error correctly
+            exitValue = PS_EXIT_PROG_ERROR;
+            break;
+        }
+    }
+
     exit(exitValue);
 }
