Index: trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches.c	(revision 26282)
+++ trunk/Ohana/src/addstar/src/find_matches.c	(revision 26283)
@@ -115,10 +115,10 @@
     /* negative dX: j is too large */
     dX = X1[i] - X2[j];
-    if (dX <= -2*RADIUS) {
+    if (dX <= -1.02*RADIUS) {
       i++;
       continue;
     }
     /* positive dX, i is too large */
-    if (dX >= 2*RADIUS) {
+    if (dX >= 1.02*RADIUS) {
       j++;
       continue;
@@ -126,9 +126,14 @@
 
     /* within match range; look for matches */
-    for (J = j; (dX > -2*RADIUS) && (J < Nave); J++) {
+    for (J = j; (dX > -1.02*RADIUS) && (J < Nave); J++) {
       dX = X1[i] - X2[J];
       dY = Y1[i] - Y2[J];
       dR = dX*dX + dY*dY;
       if (dR > RADIUS2) continue;
+
+      /*** a match is found, add to average, measure ***/
+      Nmatch ++;
+      n = N2[J];
+      N = N1[i];
 
       /* make sure there is space for next entry */
@@ -138,8 +143,4 @@
 	REALLOCATE (catalog[0].measure, Measure, NMEAS);
       }
-
-      Nmatch ++;
-      n = N2[J];
-      N = N1[i];
 
       /* add to end of measurement list */
Index: trunk/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 26282)
+++ trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 26283)
@@ -148,4 +148,7 @@
 
     /*** a match is found, add to average, measure ***/
+    Nmatch ++;
+    n = N2[Jmin];
+    N = N1[i];
 
     /* make sure there is space for next entry */
@@ -155,8 +158,4 @@
       REALLOCATE (catalog[0].measure, Measure, NMEAS);
     }
-
-    Nmatch ++;
-    n = N2[Jmin];
-    N = N1[i];
 
     /* add to end of measurement list */
Index: trunk/Ohana/src/addstar/src/find_matches_closest_refstars.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches_closest_refstars.c	(revision 26282)
+++ trunk/Ohana/src/addstar/src/find_matches_closest_refstars.c	(revision 26283)
@@ -152,4 +152,13 @@
     }
 
+    /*** a match is found, add to average, measure ***/
+    Nmatch ++;
+    n = N2[Jmin];
+    N = N1[i];
+
+    /** in replace mode, search for entry and replace values M, dM, R, D */
+    // XXX this fails for unsorted catalogs, right?
+    if (options.replace && replace_match (&catalog[0].average[n], catalog[0].measure, stars[N])) continue;
+
     /* make sure there is space for next entry */
     if (Nmeas >= NMEAS) {
@@ -158,13 +167,4 @@
       REALLOCATE (catalog[0].measure, Measure, NMEAS);
     }
-
-    /*** a match is found, add to average, measure ***/
-    Nmatch ++;
-    n = N2[Jmin];
-    N = N1[i];
-
-    /** in replace mode, search for entry and replace values M, dM, R, D */
-    // XXX this fails for unsorted catalogs, right?
-    if (options.replace && replace_match (&catalog[0].average[n], catalog[0].measure, stars[N])) continue;
 
     /* add to end of measurement list */
Index: trunk/Ohana/src/addstar/src/find_matches_refstars.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 26282)
+++ trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 26283)
@@ -38,8 +38,5 @@
 
   Nmatch = 0;
-  Nmeas = catalog[0].Nmeasure;
-  NMEAS = Nmeas + 1000;
-  ALLOCATE (next_meas, int, NMEAS);
-  REALLOCATE (catalog[0].measure, Measure, NMEAS);
+  NMEAS = Nmeas = catalog[0].Nmeasure;
   
   // current max obj ID for this catalog
@@ -91,11 +88,19 @@
   // XXX could use NREFSTAR_GROUP to do this match more quicky
   for (i = j = 0; (i < Nstars) && (j < Nave); ) {
+    if (!finite(X1[i]) || !finite(Y1[i])) { 
+      i++; 
+      continue;
+    }
+    if (!finite(X2[j]) || !finite(Y2[j])) { 
+      j++; 
+      continue;
+    }
     
     dX = X1[i] - X2[j];
-    if (dX <= -2*RADIUS) {
+    if (dX <= -1.02*RADIUS) {
       i++;
       continue;
     }
-    if (dX >= 2*RADIUS) {
+    if (dX >= 1.02*RADIUS) {
       j++;
       continue;
@@ -103,5 +108,5 @@
 
     /* negative dX: j is too large, positive dX, i is too large */
-    for (J = j; (dX > -2*RADIUS) && (J < Nave); J++) {
+    for (J = j; (dX > -1.02*RADIUS) && (J < Nave); J++) {
       dX = X1[i] - X2[J];
       dY = Y1[i] - Y2[J];
@@ -109,4 +114,5 @@
       if (dR > RADIUS2) continue;
 
+      /*** a match is found, add to average, measure ***/
       Nmatch ++;
       n = N2[J];
@@ -115,4 +121,10 @@
       /** in replace mode, search for entry and replace values M, dM, R, D */
       if (options.replace && replace_match (&catalog[0].average[n], catalog[0].measure, stars[N])) continue;
+
+      if (Nmeas == NMEAS) {
+	NMEAS = Nmeas + 1000;
+	REALLOCATE (next_meas, int, NMEAS);
+	REALLOCATE (catalog[0].measure, Measure, NMEAS);
+      }
 
       /* add to end of measurement list */
@@ -127,4 +139,8 @@
       catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N][0].average.R);
       catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N][0].average.D);
+      catalog[0].measure[Nmeas].dbFlags  = 0;
+      catalog[0].measure[Nmeas].averef   = n;
+      catalog[0].measure[Nmeas].objID    = catalog[0].average[n].objID;
+      catalog[0].measure[Nmeas].catID    = catalog[0].catID;
 
       // rationalize dR:
@@ -139,9 +155,4 @@
 	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N][0].average.R);
       }
-
-      catalog[0].measure[Nmeas].averef   = n;
-      catalog[0].measure[Nmeas].dbFlags  = 0;
-      catalog[0].measure[Nmeas].objID    = catalog[0].average[n].objID;
-      catalog[0].measure[Nmeas].catID    = catalog[0].catID;
 
       catalog[0].measure[Nmeas].t        = (TIMEREF == 0) ? stars[N][0].measure.t      : TIMEREF; /** careful : time_t vs e_time **/
@@ -181,11 +192,6 @@
       catalog[0].average[n].Nmeasure ++;
       Nmeas ++;
-      if (Nmeas == NMEAS) {
-	NMEAS = Nmeas + 1000;
-	REALLOCATE (next_meas, int, NMEAS);
-	REALLOCATE (catalog[0].measure, Measure, NMEAS);
-      }
-
-      update_coords (&catalog[0].average[n], &catalog[0].measure[0], next_meas);
+
+      // update_coords (&catalog[0].average[n], &catalog[0].measure[0], next_meas);
     }
     i++;
@@ -201,6 +207,18 @@
 
   for (i = 0; (i < Nstars) && !options.only_match; i+=NREFSTAR_GROUP) {
+    if (Nmeas >= NMEAS - NREFSTAR_GROUP) {
+      NMEAS = Nmeas + 1000;
+      REALLOCATE (next_meas, int, NMEAS);
+      REALLOCATE (catalog[0].measure, Measure, NMEAS);
+    }
+    if (Nave == NAVE) {
+      NAVE = Nave + 1000;
+      REALLOCATE (catalog[0].average, Average, NAVE);
+      REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*catalog[0].Nsecfilt);
+    }
+
     N = N1[i];
     if (stars[N][0].found > -1) continue;
+    if (!IN_REGION (stars[N][0].average.R, stars[N][0].average.D)) continue;
 
     catalog[0].average[Nave].R         	   = stars[N][0].average.R;
@@ -272,18 +290,8 @@
 
       stars[N][0].found = Nmeas;
+      next_meas[Nmeas] = -1;
       Nmeas ++;
-      if (Nmeas == NMEAS) {
-	NMEAS = Nmeas + 1000;
-	REALLOCATE (next_meas, int, NMEAS);
-	REALLOCATE (catalog[0].measure, Measure, NMEAS);
-      }
-    }
-
+    }
     Nave ++;
-    if (Nave == NAVE) {
-      NAVE = Nave + 1000;
-      REALLOCATE (catalog[0].average, Average, NAVE);
-      REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*catalog[0].Nsecfilt);
-    }
   }
       
