         linked = false;
         break;
       }
-      if (dd>max(
-          sqrt(powf(xy[ii] - xy[aa],2.0f) + powf(xy[ii+1] - xy[aa+1],2.0f)),
-          sqrt(powf(xy[jj] - xy[aa],2.0f) + powf(xy[jj+1] - xy[aa+1],2.0f))
-        )
-      ){
+      if (dd>max(dist(xy, xy, aa, ii), dist(xy, xy, jj, aa))){
         linked = false;
         break;
       }
     }
 
-    if (dd>0.0f){
+    dx /= dd;
+    dy /= dd;
 
-      dx /= dd;
-      dy /= dd;
+    mx += xy[jj];
+    my += xy[jj+1];
 
-      mx += xy[jj];
-      my += xy[jj+1];
-
-      if (linked){
-        links[10*i+link_count] = jj/2;
-        link_count += 1;
-        if (dd>spring_attract_rad){
-          sx += -dx*spring_stp;
-          sy += -dy*spring_stp;
-        }
-        else if(dd<spring_reject_rad){
-          sx += dx*spring_stp;
-          sy += dy*spring_stp;
-        }
+    if (linked){
+      links[10*i+link_count] = jj/2;
+      link_count += 1;
+      if (dd>spring_attract_rad){
+        sx += -dx*spring_stp;
+        sy += -dy*spring_stp;
       }
-      else{ // unlinked
-        sx += dx*reject_stp;
-        sy += dy*reject_stp;
+      else if(dd<spring_reject_rad){
+        sx += dx*spring_stp;
+        sy += dy*spring_stp;
       }
+    }
+    else{ // unlinked
+      sx += dx*reject_stp;
+      sy += dy*reject_stp;
     }
   }
 


  int link_count = 0;
  int cand_count = 0;
  int total_count = 0;

  bool linked;


      zk = a*nz+b;
      for (int k=0;k<zone_num[zk];k++){
        jj = 2*zone_node[zk*zone_leap+k];
        dd = dist(xy, xy, ii, jj);
        if (dd<outer_influence_rad && dd>0.0f){
          proximity[cand_count] = jj/2;
          cand_count += 1;
