Index: /trunk/Ohana/src/imregister/Makefile
===================================================================
--- /trunk/Ohana/src/imregister/Makefile	(revision 2822)
+++ /trunk/Ohana/src/imregister/Makefile	(revision 2823)
@@ -141,5 +141,5 @@
 $(BIN)/detsearch.$(ARCH)    : $(SRC)/detsearch.$(ARCH).o    $(DS) ; $(CC) $^ -o $@ $(LFLAGS)
 
-$(BIN)/imsort.$(ARCH)       : $(BIN)/imregister.$(ARCH) ; rm -f $(BIN)/imsort.$(ARCH) ; ln $(BIN)/imregister.$(ARCH) $(BIN)/imsort.$(ARCH) 
+$(BIN)/imsort.$(ARCH)       : $(BIN)/imregister.$(ARCH) ; rm -f $(BIN)/imsort.$(ARCH) ; cp $(BIN)/imregister.$(ARCH) $(BIN)/imsort.$(ARCH) 
 
 $(BIN)/spsearch.$(ARCH)     : $(SRC)/spsearch.$(ARCH).o     $(SS) ; $(CC) $^ -o $@ $(LFLAGS)
Index: /trunk/Ohana/src/imregister/base/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/imregister/base/ConfigInit.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/base/ConfigInit.c	(revision 2823)
@@ -75,4 +75,8 @@
   sprintf (PtolemyFifo, "%s.source", ElixirBase);
 
+  if (!ScanConfig (config, "CONNECT", "%s",  0, CONNECT)) {
+    sprintf (CONNECT, "/usr/bin/rsh");
+  }
+
   /* load Detrend Alt Databases paths */
   NDB = 10;
Index: /trunk/Ohana/src/imregister/detrend/db.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/db.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/detrend/db.c	(revision 2823)
@@ -3,8 +3,6 @@
 
 /* variables which describe the db */
-char *dBPath;
+FILE *f;
 char *dBFile;
-char *dBTrash;
-FILE *f;
 Header header;
 Header theader;
@@ -13,5 +11,31 @@
 DetReg *image;
 int Nimage;
-int lockstate, dbstate = LCK_UNLOCK;
+int lockstate;
+int dbstate;
+
+/* special variables unique to detrend.db */
+char *dBPath;
+char *dBTrash;
+
+void init_db () {
+  f              = NULL;
+  dBFile         = NULL;
+  header.buffer  = NULL;
+  matrix.buffer  = NULL;
+  theader.buffer = NULL;
+  table.buffer   = NULL;
+  table.header   = &theader;
+  image          = NULL;
+  Nimage         = 0;
+  lockstate      = LCK_EMPTY;
+  dbstate        = LCK_UNLOCK;
+  dBPath         = NULL;
+  dBTrash        = NULL;
+}
+
+char *get_dBPath () {
+  dBPath = DetrendDB;
+  return (dBPath);
+}
 
 DetReg *get_images (int *N) {
@@ -24,36 +48,23 @@
   int Nbytes;
 
-  free (image);
-
+  /* assign pointers to new data block */
+  Nimage = Nnew;
+  image = new;
+  if (image == NULL) Nnew = 0;
+
+  /* update header, table structures */
   fits_modify (table.header, "NAXIS2", "%d", 1, Nnew);
   table.header[0].Naxis[1] = Nnew;
+  table.buffer = (char *) image;
+
+  /* add padding space to buffer */
   Nbytes = fits_matrix_size (table.header);
-
-  Nimage = Nnew;
-  image = new;
-  REALLOCATE (image, DetReg, Nbytes);
-  table.buffer = (char *) image;
-  return (TRUE);
-}
-
-char *get_dBPath () {
-  dBPath = DetrendDB;
-  return (dBPath);
-}
-
-int delete_image (DetReg *item) {
-  
-  int status;
-  char line[256];
-
-  if (output.verbose) fprintf (stderr, "deleting %s\n", item[0].filename);
-  sprintf (line, "mv -f %s/%s %s", dBPath, item[0].filename, dBTrash);
-  status = system (line);
-  if (status) fprintf (stderr, "trouble moving %s to trash (%s)\n", item[0].filename, dBTrash);
-
-  if (status) 
-    return (FALSE);
-  else 
-    return (TRUE);
+  if (Nbytes > 0) {
+    REALLOCATE (table.buffer, char, Nbytes);
+    image = (DetReg *) table.buffer;
+  }
+  table.size = Nbytes;
+
+  return (TRUE);
 }
 
@@ -91,4 +102,5 @@
   }
   lockstate = (output.Modify || output.Delete) ? LCK_HARD : LCK_SOFT;
+  header.buffer = matrix.buffer = theader.buffer = table.buffer = (char *) NULL;
 
   /* lock database (soft) */
@@ -102,8 +114,7 @@
 
   /* load in table data */
-  table.header = &theader;
   if (!fits_fread_header (f, &header))                     escape (UNLOCK, "ERROR: can't read primary header"); 
-  if (!fits_fread_matrix (f, &matrix, &header))            escape (UNLOCK, "ERROR: can't read primary header");
-  if (!fits_fread_ftable  (f, &table, "DETREND_DATABASE")) escape (UNLOCK, "ERROR: can't read primary header");
+  if (!fits_fread_matrix (f, &matrix, &header))            escape (UNLOCK, "ERROR: can't read primary matrix");
+  if (!fits_fread_ftable  (f, &table, "DETREND_DATABASE")) escape (UNLOCK, "ERROR: can't read table");
 
   /* convert to internal format */
@@ -113,13 +124,15 @@
   Nimage = Ny;
 
-  if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg")) escape (UNLOCK, "ERROR: can't read primary header"); 
+  if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg")) 
+    escape (UNLOCK, "ERROR: can't convert data"); 
+
   return (1);
 }
-
 
 /* save complete db file */
 int save_db () {
 
-  if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg")) escape (UNLOCK, "ERROR: can't convert from native format");
+  if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg")) 
+    escape (UNLOCK, "ERROR: can't convert from native format");
 
   /* write all images to file */
@@ -130,5 +143,8 @@
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
+
+  fits_free_table  (&table);
   close_db ();
+
   return (TRUE);
 }
@@ -169,8 +185,13 @@
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't update db");
+
+  fits_free_table  (&table);
+  fits_free_vtable (&vtable);
   close_db ();
-  return (TRUE);
-}
-
+
+  return (TRUE);
+}
+
+/* add new entries to db, close db */
 int append_db (DetReg *new, int Nnew) {
 
@@ -182,4 +203,5 @@
   ConvertStruct ((char *) new, sizeof (DetReg), Nnew, "detreg");
   fits_vadd_rows (&vtable, (char *) new, Nnew, sizeof(DetReg));
+  Nimage = theader.Naxis[1];
 
   /* write subset to file */
@@ -189,5 +211,9 @@
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't append to db");
   if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't append to db");
+
+  fits_free_table (&table);
+  fits_free_vtable (&vtable);
   close_db ();
+
   return (TRUE);
 }
@@ -208,7 +234,14 @@
 
 int close_db () {
-  if (dbstate == LCK_UNLOCK) return (TRUE);
-  if (dbstate == LCK_EMPTY) return (TRUE);
+
+  /* these are safe even if db is empty */
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  /* don't free table: db data may still be used */
+
   fclearlockfile (dBFile, f, lockstate, &dbstate);
+  if ((Nimage == 0) && (lockstate != LCK_SOFT)) unlink (dBFile);
+
   return (TRUE);
 }  
@@ -220,2 +253,19 @@
   exit (1);
 }
+
+int delete_image (DetReg *item) {
+  
+  int status;
+  char line[256];
+
+  if (output.verbose) fprintf (stderr, "deleting %s\n", item[0].filename);
+  sprintf (line, "mv -f %s/%s %s", dBPath, item[0].filename, dBTrash);
+  status = system (line);
+  if (status) fprintf (stderr, "trouble moving %s to trash (%s)\n", item[0].filename, dBTrash);
+
+  if (status) 
+    return (FALSE);
+  else 
+    return (TRUE);
+}
+
Index: /trunk/Ohana/src/imregister/detrend/delete.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/delete.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/detrend/delete.c	(revision 2823)
@@ -2,5 +2,5 @@
 # include "detrend.h"
 
-int DeleteSubset (Match *match, int Nmatch) {
+void DeleteSubset (Match *match, int Nmatch) {
 
   int i, j;
@@ -10,38 +10,39 @@
   image = get_images (&Nimage);
 
-  ALLOCATE (keep, int, Nimage);
+  ALLOCATE (keep, int, MAX (Nimage, 1));
   for (i = 0; i < Nimage; i++) keep[i] = TRUE;
+  fprintf (stderr, "total of %d detrend images\n", Nimage);
 
   Ndel = 0;
-  /* list matched images */
   for (j = 0; j < Nmatch; j++) {
     i = match[j].image;
     if (i == -1) continue;
     keep[i] = FALSE;
-
+    Ndel ++;
     delete_image (&image[i]);
-    Ndel ++;
   }
+  fprintf (stderr, "delete %d images\n", Ndel);
   
   if (Ndel == 0) { 
     fprintf (stderr, "SUCCESS\n");
+    close_db ();
     exit (0);
   }
 
   /* create new data list */
-  ALLOCATE (subset, DetReg, Nimage);
-  Nsubset = 0;
-  for (i = 0; i < Nimage; i++) {
-    if (keep[i]) {
-      subset[Nsubset] = image[i];
-      Nsubset ++;
-    }
+  Nsubset = Nimage - Ndel;
+  ALLOCATE (subset, DetReg, MAX (1, Nsubset));
+  fprintf (stderr, "keeping %d images\n", Nsubset);
+  for (j = i = 0; i < Nimage; i++) {
+    if (!keep[i]) continue;
+    subset[j] = image[i];
+    j ++;
   }
 
+  free (keep);
+  free (image);
   set_images (subset, Nsubset);
 
   save_db ();
-  close_db ();
-
   fprintf (stderr, "SUCCESS\n");
   exit (0);
Index: /trunk/Ohana/src/imregister/imreg/SetSignals.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/SetSignals.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/imreg/SetSignals.c	(revision 2823)
@@ -93,5 +93,5 @@
 
   /* send signal to remote machine */
-  if (!rconnect (machine, "/usr/bin/rsh", &rsock, &wsock)) {
+  if (!rconnect (machine, CONNECT, &rsock, &wsock)) {
     fprintf (stderr, "can't make connection to machine %s to kill process %d\n", machine, pid);
     exit (1);
@@ -100,5 +100,5 @@
   write (wsock, line, strlen (line));
 
-  for (i = 0; i < 100; i++) {
+  for (i = 0; i < 300; i++) {
     if (stat (pidfile, &filestat) == -1) exit (0);
     usleep (100000);
Index: /trunk/Ohana/src/imregister/imreg/args.imregister.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/args.imregister.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/imreg/args.imregister.c	(revision 2823)
@@ -72,4 +72,8 @@
     sprintf (PIDFILE, "%s/.%s.pid", path, file);
 
+    /* create db.log and db.bfr */
+    sprintf (TempDB, "%s.bfr", ImageDB);
+    sprintf (LogFile, "%s.log", ImageDB);
+    
     /* check for daemon mode */
     if ((N = get_argument (argc, argv, "-daemon"))) {
Index: /trunk/Ohana/src/imregister/imreg/db.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/db.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/imreg/db.c	(revision 2823)
@@ -3,6 +3,6 @@
 
 /* variables which describe the db */
-char *dBFile = (char *) NULL;
 FILE *f;
+char *dBFile;
 Header header;
 Header theader;
@@ -12,5 +12,19 @@
 int Nimage;
 int lockstate;
-int dbstate = LCK_UNLOCK;
+int dbstate;
+
+void init_db () {
+  f              = NULL;
+  dBFile         = NULL;
+  header.buffer  = NULL;
+  matrix.buffer  = NULL;
+  theader.buffer = NULL;
+  table.buffer   = NULL;
+  table.header   = &theader;
+  image          = NULL;
+  Nimage         = 0;
+  lockstate      = LCK_EMPTY;
+  dbstate        = LCK_UNLOCK;
+}
 
 int set_db (char *filename) {
@@ -29,22 +43,21 @@
 
   int Nbytes;
-  char *data;
 
   /* assign pointers to new data block */
-  free (image);
   Nimage = Nnew;
   image = new;
+  if (image == NULL) Nnew = 0;
 
   /* update header, table structures */
   fits_modify (table.header, "NAXIS2", "%d", 1, Nnew);
   table.header[0].Naxis[1] = Nnew;
+  table.buffer = (char *) image;
+
+  /* add padding space to buffer */
   Nbytes = fits_matrix_size (table.header);
-
-  /* add padding space to buffer */
-  data = (char *) image;
-  REALLOCATE (data, char, Nbytes);
-  image = (RegImage *) data;
-
-  table.buffer = (char *) image;
+  if (Nbytes > 0) {
+    REALLOCATE (table.buffer, char, Nbytes);
+    image = (RegImage *) table.buffer;
+  }
   table.size = Nbytes;
 
@@ -67,4 +80,5 @@
   f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
   if (dbstate == LCK_EMPTY) return (0);
+
   if (f == (FILE *) NULL) {
     fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
@@ -73,5 +87,4 @@
 
   /* init & load in table data */
-  table.header = &theader;
   if (!fits_fread_header (f, &header))                  escape (UNLOCK, "ERROR: can't read primary header"); 
   if (!fits_fread_matrix (f, &matrix, &header))         escape (UNLOCK, "ERROR: can't read primary matrix");
@@ -104,10 +117,7 @@
   if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
 
-  fits_free_header (&header);
-  fits_free_matrix (&matrix);
-  fits_free_header (&theader);
   fits_free_table  (&table);
-
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
+  close_db ();
+
   return (TRUE);
 }
@@ -149,14 +159,12 @@
   if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't update db");
 
-  fits_free_header (&header);
-  fits_free_matrix (&matrix);
-  fits_free_header (&theader);
+  fits_free_table  (&table);
   fits_free_vtable (&vtable);
-  fits_free_table  (&table);
-
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
-  return (TRUE);
-}
-
+  close_db ();
+
+  return (TRUE);
+}
+
+/* add new entries to db, close db */
 int append_db (RegImage *new, int Nnew) {
 
@@ -168,4 +176,5 @@
   ConvertStruct ((char *) new, sizeof (RegImage), Nnew, "regimage");
   fits_vadd_rows (&vtable, (char *) new, Nnew, sizeof(RegImage));
+  Nimage = theader.Naxis[1];
 
   /* write subset to file */
@@ -176,10 +185,8 @@
   if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't append to db");
 
-  fits_free_header (&header);
-  fits_free_matrix (&matrix);
-  fits_free_header (&theader);
+  fits_free_table (&table);
   fits_free_vtable (&vtable);
-
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
+  close_db ();
+
   return (TRUE);
 }
@@ -201,28 +208,13 @@
 int close_db () {
 
-  if (dbstate == LCK_EMPTY) return (TRUE);
-
-  fits_free_header (&header);
-  fits_free_matrix (&matrix);
-  fits_free_header (&theader);
-  /* fits_free_table  (&table); */
-
-  if (dbstate == LCK_UNLOCK) return (TRUE);
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
-  return (TRUE);
-}  
-
-int close_lock_db () {
-
-  if (dbstate == LCK_EMPTY) return (TRUE);
-
+  /* these are safe even if db is empty */
   fits_free_header (&header);
   fits_free_matrix (&matrix);
   fits_free_header (&theader);
   /* don't free table: db data may still be used */
-  /* fits_free_table  (&table); */
-
-  if (dbstate == LCK_UNLOCK) return (TRUE);
-  fclearlockfile (dBFile, f, LCK_XCLD, &dbstate);
+
+  fclearlockfile (dBFile, f, lockstate, &dbstate);
+  if ((Nimage == 0) && (lockstate != LCK_SOFT)) unlink (dBFile);
+
   return (TRUE);
 }  
Index: /trunk/Ohana/src/imregister/imreg/delete.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/delete.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/imreg/delete.c	(revision 2823)
@@ -5,5 +5,5 @@
 
   int i, j;
-  int *keep, Nbad, Nimage, Nsubset;
+  int *keep, Ndel, Nimage, Nsubset;
   RegImage *image, *subset;
 
@@ -14,14 +14,21 @@
   fprintf (stderr, "total of %d images\n", Nimage);
 
-  Nbad = 0;
+  Ndel = 0;
   for (i = 0; i < Nmatch; i++) {
     j = match[i];
     if (j == -1) continue;
     keep[j] = FALSE;
-    Nbad ++;
+    Ndel ++;
   }
-  fprintf (stderr, "delete %d images\n", Nbad);
+  fprintf (stderr, "delete %d images\n", Ndel);
 
-  Nsubset = Nimage - Nbad;
+  if (Ndel == 0) { 
+    fprintf (stderr, "SUCCESS\n");
+    close_db ();
+    exit (0);
+  }
+
+  /* create new data list */
+  Nsubset = Nimage - Ndel;
   ALLOCATE (subset, RegImage, MAX (1, Nsubset));
   fprintf (stderr, "keeping %d images\n", Nsubset);
@@ -29,9 +36,9 @@
     if (!keep[i]) continue;
     subset[j] = image[i];
-    /* this or memcpy ? */
     j++;
   }
 
   free (keep);
+  free (image);
   set_images (subset, Nsubset);
 
Index: /trunk/Ohana/src/imregister/imreg/imregclient.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/imregclient.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/imreg/imregclient.c	(revision 2823)
@@ -6,10 +6,6 @@
   FILE *f;
   RegImage *image;
-  char *TempDB;
   int i, Nentry, Nslice, status, tmpint;
   float *dtime;
-
-  ALLOCATE (TempDB, char, strlen (ImageDB) + 10);
-  sprintf (TempDB, "%s.bfr", ImageDB);
 
   Nentry = 0;
Index: /trunk/Ohana/src/imregister/imreg/rconnect.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/rconnect.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/imreg/rconnect.c	(revision 2823)
@@ -63,7 +63,8 @@
   /* try to get evidence connection is alive - wait upto a few seconds */
   status = -1;
-  for (i = 0; (i < 100) && (status == -1); i++) {
+  for (i = 0; (i < 300) && (status == -1); i++) {
     fcntl (*rsock, F_SETFL, O_NONBLOCK);
     status = SockScan ("PTOLEMY STARTED", &fifo, *rsock);
+    if (!(i % 30)) fprintf (stderr, ".");
     if (status == 0) {
       fprintf (stderr, "socket closed unexpectedly\n");
@@ -73,5 +74,5 @@
     }
   }
-  if (i == 100) {
+  if (i == -1) {
     fprintf (stderr, "timeout while connecting\n");
     close (*wsock);
Index: /trunk/Ohana/src/imregister/include/detrend.h
===================================================================
--- /trunk/Ohana/src/imregister/include/detrend.h	(revision 2822)
+++ /trunk/Ohana/src/imregister/include/detrend.h	(revision 2823)
@@ -102,5 +102,5 @@
 
 int ModifySubset (Match *match, int Nmatch);
-int DeleteSubset (Match *match, int Nmatch);
+void DeleteSubset (Match *match, int Nmatch);
 int usage ();
 Criteria *MosaicCriteria (Criteria base, char *filename, int *ncrit);
Index: /trunk/Ohana/src/imregister/include/imregister.h
===================================================================
--- /trunk/Ohana/src/imregister/include/imregister.h	(revision 2822)
+++ /trunk/Ohana/src/imregister/include/imregister.h	(revision 2823)
@@ -19,4 +19,7 @@
 char TransDB[256];
 char ImPhotDB[256];
+char TempDB[256];
+char LogFile[256];
+char CONNECT[64];
 
 int  NDetrendAltDB;
Index: /trunk/Ohana/src/imregister/photreg/db.c
===================================================================
--- /trunk/Ohana/src/imregister/photreg/db.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/photreg/db.c	(revision 2823)
@@ -3,6 +3,6 @@
 
 /* variables which describe the db */
-char *dBFile = (char *) NULL;
 FILE *f;
+char *dBFile;
 Header header;
 Header theader;
@@ -12,6 +12,20 @@
 int Nimage;
 int lockstate;
-int dbstate = LCK_UNLOCK;
-static char *BinaryName = (char *) NULL;
+int dbstate;
+char *BinaryName = NULL;
+
+void init_db () {
+  f              = NULL;
+  dBFile         = NULL;
+  header.buffer  = NULL;
+  matrix.buffer  = NULL;
+  theader.buffer = NULL;
+  table.buffer   = NULL;
+  table.header   = &theader;
+  image          = NULL;
+  Nimage         = 0;
+  lockstate      = LCK_EMPTY;
+  dbstate        = LCK_UNLOCK;
+}
 
 /* photreg vs transreg database selection */
@@ -60,17 +74,19 @@
 
   /* assign pointers to new data block */
-  free (image);
   Nimage = Nnew;
   image = new;
+  if (image == NULL) Nnew = 0;
 
   /* update header, table structures */
   fits_modify (table.header, "NAXIS2", "%d", 1, Nnew);
   table.header[0].Naxis[1] = Nnew;
+  table.buffer = (char *) image;
+
+  /* add padding space to buffer */
   Nbytes = fits_matrix_size (table.header);
-
-  /* add padding space to buffer */
-  table.buffer = (char *) image;
-  REALLOCATE (table.buffer, char, Nbytes);
-  image = (PhotPars *) table.buffer;
+  if (Nbytes > 0) {
+    REALLOCATE (table.buffer, char, Nbytes);
+    image = (PhotPars *) table.buffer;
+  }
   table.size = Nbytes;
 
@@ -89,4 +105,5 @@
 
   lockstate = (output.modify || output.delete) ? LCK_HARD : LCK_SOFT;
+  header.buffer = matrix.buffer = theader.buffer = table.buffer = (char *) NULL;
 
   if (dBFile == (char *) NULL) {
@@ -98,4 +115,5 @@
   f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
   if (dbstate == LCK_EMPTY) return (0);
+
   if (f == (FILE *) NULL) {
     fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
@@ -104,5 +122,4 @@
 
   /* load in table data */
-  table.header = &theader;
   if (!fits_fread_header (f, &header))            escape (UNLOCK, "ERROR: can't read primary header"); 
   if (!fits_fread_matrix (f, &matrix, &header))   escape (UNLOCK, "ERROR: can't read primary header");
@@ -116,5 +133,5 @@
 
   if (!ConvertStruct ((char *) image, sizeof (PhotPars), Nimage, "photpars"))
-    escape (UNLOCK, "ERROR: can't read primary header"); 
+    escape (UNLOCK, "ERROR: can't convert data"); 
 
   return (1);
@@ -139,12 +156,12 @@
   /* lock database */
   f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
+  if (dbstate == LCK_EMPTY) return (0);
+
   if (f == (FILE *) NULL) {
     fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
     exit (1);
   }
-  if (dbstate == LCK_EMPTY) return (0);
 
   /* load in table data */
-  table.header = &theader;
   if (!fits_fread_header (f, &header))            escape (UNLOCK, "ERROR: can't read primary header"); 
   if (!fits_fread_matrix (f, &matrix, &header))   escape (UNLOCK, "ERROR: can't read primary header");
@@ -195,5 +212,8 @@
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
+
+  fits_free_table  (&table);
+  close_db ();
+
   return (TRUE);
 }
@@ -234,5 +254,9 @@
   if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
   if (!fits_fwrite_vtable  (f, &vtable))  escape (LOCK, "ERROR: can't update db");
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
+
+  fits_free_table  (&table);
+  fits_free_vtable (&vtable);
+  close_db ();
+
   return (TRUE);
 }
@@ -247,4 +271,5 @@
   ConvertStruct ((char *) new, sizeof (PhotPars), Nnew, "photpars");
   fits_vadd_rows (&vtable, (char *) new, Nnew, sizeof(PhotPars));
+  Nimage = theader.Naxis[1];
 
   /* write subset to file */
@@ -255,5 +280,8 @@
   if (!fits_fwrite_vtable  (f, &vtable))  escape (LOCK, "ERROR: can't append to db");
 
-  fclearlockfile (dBFile, f, lockstate, &dbstate);
+  fits_free_table (&table);
+  fits_free_vtable (&vtable);
+  close_db ();
+
   return (TRUE);
 }
@@ -274,12 +302,14 @@
 
 int close_db () {
-  if (dbstate == LCK_UNLOCK) return (TRUE);
+
+  /* these are safe even if db is empty */
+  fits_free_header (&header);
+  fits_free_matrix (&matrix);
+  fits_free_header (&theader);
+  /* don't free table: db data may still be used */
+
   fclearlockfile (dBFile, f, lockstate, &dbstate);
-  return (TRUE);
-}  
-
-int close_lock_db () {
-  if (dbstate == LCK_UNLOCK) return (TRUE);
-  fclearlockfile (dBFile, f, LCK_XCLD, &dbstate);
+  if ((Nimage == 0) && (lockstate != LCK_SOFT)) unlink (dBFile);
+
   return (TRUE);
 }  
Index: /trunk/Ohana/src/imregister/photreg/delete.c
===================================================================
--- /trunk/Ohana/src/imregister/photreg/delete.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/photreg/delete.c	(revision 2823)
@@ -5,5 +5,5 @@
 
   int i, j;
-  int *keep, Nbad, Nphotdata, Nsubset;
+  int *keep, Ndel, Nphotdata, Nsubset;
   PhotPars *photdata, *subset;
 
@@ -14,14 +14,21 @@
   fprintf (stderr, "total of %d photdata\n", Nphotdata);
 
-  Nbad = 0;
+  Ndel = 0;
   for (i = 0; i < Nmatch; i++) {
     j = match[i];
     if (j == -1) continue;
     keep[j] = FALSE;
-    Nbad ++;
+    Ndel ++;
   }
-  fprintf (stderr, "delete %d photdata\n", Nbad);
+  fprintf (stderr, "delete %d photdata\n", Ndel);
 
-  Nsubset = Nphotdata - Nbad;
+  if (Ndel == 0) { 
+    fprintf (stderr, "SUCCESS\n");
+    close_db ();
+    exit (0);
+  }
+
+  /* create new data list */
+  Nsubset = Nphotdata - Ndel;
   ALLOCATE (subset, PhotPars, MAX (1, Nsubset));
   fprintf (stderr, "keeping %d photdata\n", Nsubset);
@@ -29,9 +36,9 @@
     if (!keep[i]) continue;
     subset[j] = photdata[i];
-    /* this or memcpy ? */
     j++;
   }
 
   free (keep);
+  free (photdata);
   set_images (subset, Nsubset);
 
Index: /trunk/Ohana/src/imregister/src/detregister.c
===================================================================
--- /trunk/Ohana/src/imregister/src/detregister.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/detregister.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "detrend.h"
-static char *version = "detregister $Revision: 3.0 $";
+static char *version = "detregister $Revision: 3.1 $";
 
 int main (int argc, char **argv) {
@@ -16,4 +16,5 @@
   SaveEntry (argv[1], &newdata, descriptor.imageID);
 
+  init_db ();
   status = load_db ();
   if (!status) {
Index: /trunk/Ohana/src/imregister/src/detsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/detsearch.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/detsearch.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "detrend.h"
-static char *version = "detsearch $Revision: 3.2 $";
+static char *version = "detsearch $Revision: 3.3 $";
 
 int main (int argc, char **argv) {
@@ -15,4 +15,5 @@
   if (output.Criteria) PrintCriteria ();
 	
+  init_db ();
   status = load_db ();
   if (!status) {
Index: /trunk/Ohana/src/imregister/src/imregister.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imregister.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/imregister.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imregister $Revision: 3.3 $";
+static char *version = "imregister $Revision: 3.4 $";
 
 int main (int argc, char **argv) {
@@ -12,4 +12,5 @@
   args (argc, argv);
   image = iminfo (argv[1]);
+  init_db ();
 
   Nnew = 0;
Index: /trunk/Ohana/src/imregister/src/imregtable.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imregtable.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/imregtable.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imregtable $Revision: 3.3 $";
+static char *version = "imregtable $Revision: 3.4 $";
 
 int main (int argc, char **argv) {
@@ -16,4 +16,5 @@
   ConfigCamera ();
   ConfigFilter ();
+  init_db ();
   
   if (argc != 2) {
Index: /trunk/Ohana/src/imregister/src/imsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imsearch.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/imsearch.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imsearch $Revision: 3.2 $";
+static char *version = "imsearch $Revision: 3.3 $";
 
 int main (int argc, char **argv) {
@@ -10,4 +10,5 @@
   get_version (argc, argv, version);
   args (argc, argv);
+  init_db ();
 
   set_db (ImageDB);
Index: unk/Ohana/src/imregister/src/imsort.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imsort.c	(revision 2822)
+++ 	(revision )
@@ -1,57 +1,0 @@
-# include "imregister.h"
-# include "imreg.h"
-static char *version = "imsort $Revision: 3.2 $";
-
-int main (int argc, char **argv) {
- 
-  RegImage *image, *pimage;
-  int i, status, Nnew;
-  float *dtime;
-
-  get_version (argc, argv, version);
-  args (argc, argv);
-  image = iminfo (argv[1]);
-  
-  /* define new entries (pimage, Nnew) */
-  switch (image[0].mode) {
-  case M_CUBE:
-    Nnew = image[0].ccd + 1;
-    ALLOCATE (pimage, RegImage, Nnew);
-    pimage[0] = image[0];
-    dtime = (float *)&image[0].junk[0];
-    for (i = 1; i < Nnew; i++) {
-      pimage[i] = image[0]; 
-      pimage[i].ccd = i - 1;
-      pimage[i].mode = M_SLICE;
-      pimage[i].obstime += *dtime*pimage[i].ccd;
-    }
-    break;
-  case M_MEF:
-    Nnew = image[0].ccd;
-    ALLOCATE (pimage, RegImage, Nnew);
-    for (i = 0; i < Nnew; i++) {
-      pimage[i] = image[0]; 
-      pimage[i].ccd = i;
-    }
-    break;
-  case M_SPLIT:
-  case M_SINGLE:
-    Nnew = 1;
-    pimage = &image[0];
-    break;
-  }
-  
-  set_db (ImageDB);
-
-  if (!NoReg) {
-    status = load_db ();
-    if (!status) {
-      create_db ();
-    }
-    append_db (pimage, Nnew);
-  }
-
-  fprintf (stderr, "SUCCESS: registered %s\n", argv[1]);
-  exit (0);
-}
-
Index: /trunk/Ohana/src/imregister/src/imstatreg.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 2823)
@@ -1,5 +1,6 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imstatreg $Revision: 3.7 $";
+static char *version = "imstatreg $Revision: 3.8 $";
+# define DEBUG 0
 
 int main (int argc, char **argv) {
@@ -7,20 +8,15 @@
   RegImage *image;
   int *match, Nmatch, status, child, Nentry;
-  char *TempDB, *LogFile;
   FILE *f;
 
   get_version (argc, argv, version);
   args (argc, argv);
+  init_db ();
 
   if (CLIENT) imregclient (argv[1], argv[2], argv[3]);
 
-  /* create db.log and db.bfr */
-  ALLOCATE (TempDB, char, strlen (ImageDB) + 10);
-  sprintf (TempDB, "%s.bfr", ImageDB);
-  ALLOCATE (LogFile, char, strlen (ImageDB) + 10);
-  sprintf (LogFile, "%s.log", ImageDB);
-
   SetSignals ();
 
+# if (!DEBUG)
   /* fork in background */
   if ((child = fork ())) {
@@ -32,8 +28,10 @@
     exit (0);
   }
+# endif
 
   /* child process, check for process */
   ConfigPID (PIDFILE);
 
+# if (!DEBUG)
   /* redirect stderr, stdout to logfile */
   f = freopen (LogFile, "a", stdout);
@@ -44,9 +42,12 @@
     f = freopen (LogFile, "a", stderr);
   }
+# else
+  f = stderr;
+# endif
 
   /* start loop */
   while (1) {
 
-    /* check / load temporary database */
+    /* check / load / delete temporary database */
     set_db (TempDB);
     status = load_db ();
@@ -58,7 +59,8 @@
     image = get_images (&Nentry);
     print_db_status ("temporary database read");
-    close_lock_db (); 
-    /* temp DB closed, hardlock on temp DB remains */
-      
+    status = set_images (NULL, 0);
+    close_db (); 
+    print_db_status ("temporary database closed");
+ 
     /* check / load main database */
     set_db (ImageDB);
@@ -67,4 +69,14 @@
       print_db_status ("main database empty");
       close_db (); 
+      /* add temp data back to temp database */
+      print_db_status ("resave temporary data");
+      set_db (TempDB);
+      status = load_db ();
+      if (!status) {
+	print_db_status ("create new temporary database");
+	create_db ();
+      }
+      append_db (image, Nentry);
+      print_db_status ("temporary database resaved");
       goto next;
     }
@@ -75,6 +87,6 @@
     print_db_status ("main database read");
     update_db (match, Nmatch);
-    free (match);
-    free (image);
+    if (match != NULL) free (match);
+    if (image != NULL) free (image);
       
     /* clear temp DB hardlock */
Index: /trunk/Ohana/src/imregister/src/photreg.c
===================================================================
--- /trunk/Ohana/src/imregister/src/photreg.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/photreg.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "photreg.h"
-static char *version = "photreg $Revision: 1.2 $";
+static char *version = "photreg $Revision: 1.3 $";
 
 int main (int argc, char **argv) {
@@ -12,4 +12,5 @@
   regargs (argc, argv, &newdata);
 
+  init_db ();
   status = load_db ();
   if (!status) {
Index: /trunk/Ohana/src/imregister/src/photsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/photsearch.c	(revision 2822)
+++ /trunk/Ohana/src/imregister/src/photsearch.c	(revision 2823)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "photreg.h"
-static char *version = "photsearch $Revision: 1.2 $";
+static char *version = "photsearch $Revision: 1.3 $";
 
 int main (int argc, char **argv) {
@@ -11,5 +11,6 @@
   get_version (argc, argv, version);
   args (argc, argv);
-  
+
+  init_db ();
   status = load_db ();
   if (!status) {
