Index: trunk/ppStack/src/ppStack.c
===================================================================
--- trunk/ppStack/src/ppStack.c	(revision 23341)
+++ trunk/ppStack/src/ppStack.c	(revision 27004)
@@ -15,21 +15,10 @@
 int main(int argc, char *argv[])
 {
-    psExit exitValue = PS_EXIT_SUCCESS; // Exit value
+    psLibInit(NULL);
     psTimerStart(TIMER_NAME);
     psTimerStart("PPSTACK_STEPS");
-    psLibInit(NULL);
 
     pmConfig *config = pmConfigRead(&argc, argv, PPSTACK_RECIPE); // Configuration
     if (!config) {
-        psErrorStackPrint(stderr, "Error reading configuration.");
-        exitValue = PS_EXIT_CONFIG_ERROR;
-        goto die;
-    }
-
-    (void) psTraceSetLevel("ppStack", 5);
-
-    if (!ppStackArgumentsSetup(argc, argv, config)) {
-        psErrorStackPrint(stderr, "Error reading arguments.\n");
-        exitValue = PS_EXIT_CONFIG_ERROR;
         goto die;
     }
@@ -38,30 +27,28 @@
 
     if (!pmModelClassInit()) {
-        psErrorStackPrint(stderr, "Error initialising model classes.\n");
-        exitValue = PS_EXIT_PROG_ERROR;
+        psError(PPSTACK_ERR_PROG, false, "Unable to initialise model classes.");
         goto die;
     }
 
     if (!psphotInit()) {
-        psErrorStackPrint(stderr, "Error initialising psphot.\n");
-        exitValue = PS_EXIT_PROG_ERROR;
+        psError(PPSTACK_ERR_PROG, false, "Error initialising psphot.");
+        goto die;
+    }
+
+    (void)psTraceSetLevel("ppStack", 5);
+
+    if (!ppStackArgumentsSetup(argc, argv, config)) {
         goto die;
     }
 
     if (!ppStackCamera(config)) {
-        psErrorStackPrint(stderr, "Error setting up input files.\n");
-        exitValue = PS_EXIT_CONFIG_ERROR;
         goto die;
     }
 
     if (!ppStackArgumentsParse(config)) {
-        psErrorStackPrint(stderr, "Error reading arguments.\n");
-        exitValue = PS_EXIT_CONFIG_ERROR;
         goto die;
     }
 
     if (!ppStackLoop(config)) {
-        psErrorStackPrint(stderr, "Error performing combination.\n");
-        exitValue = PS_EXIT_DATA_ERROR;
         goto die;
     }
@@ -78,4 +65,46 @@
     psLibFinalize();
     pmVisualClose();
+
+    psExit exitValue = PS_EXIT_SUCCESS;        // Exit value for program
+    psErrorCode errorCode = psErrorCodeLast(); // Error code
+    if (errorCode != PS_ERR_NONE) {
+        psErrorStackPrint(stderr, "Unable to perform stack.");
+        switch (errorCode) {
+          case PPSTACK_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 PPSTACK_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 PPSTACK_ERR_ARGUMENTS:
+          case PPSTACK_ERR_CONFIG:
+            exitValue = PS_EXIT_CONFIG_ERROR;
+            break;
+          case PPSTACK_ERR_PSF:
+          case PPSTACK_ERR_REJECTED:
+          case PPSTACK_ERR_DATA:
+            exitValue = PS_EXIT_DATA_ERROR;
+            break;
+          case PS_ERR_UNEXPECTED_NULL:
+          case PS_ERR_PROGRAMMING:
+          case PPSTACK_ERR_NOT_IMPLEMENTED:
+          case PPSTACK_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);
 }
