 if (tid == 0) { start = clock(); }
 #endif
 
-	  if (obj_idx < num_points) {
-
       for (int i = 0; i < s_per_time; i++) {
-      if ((dist = dist_square(num_coords, num_points, points, obj_idx, 
-            &s_clusters[i * num_coords]))
-			  		< min_dist) {
-				  min_dist = dist;
-				  new_cluster_idx = i;
-	  		}
-		  }
+        if ((dist = dist_square(num_coords, num_points, points, obj_idx, 
+              &s_clusters[i * num_coords]))
+			  	  	< min_dist) {
+				    min_dist = dist;
+			  	  new_cluster_idx = i;
+	      }
+	  	}
     }
-  }
-
-  // process the remaining clusters.
-  // The final loop is unrolled to avoid an extra comparison in the previous loops
-  for (int t = times * s_per_time; t < num_clusters; t ++) {
-
-    for (int i = tid; i < (num_clusters - times * s_per_time) * num_coords; i ++) {
-      s_clusters[i] = clusters[times * s_per_time + i];
-    }
-
-    __syncthreads();
-
-    if (obj_idx < num_points) {
+
+    // process the remaining clusters.
+    // The final loop is unrolled to avoid an extra comparison in the previous loops
+    for (int t = times * s_per_time; t < num_clusters; t ++) {
+
+      for (int i = tid; i < (num_clusters - times * s_per_time) * num_coords; i ++) {
+        s_clusters[i] = clusters[times * s_per_time + i];
+      }
+
+      __syncthreads();
 
       for (int i = 0; i < num_clusters - times * s_per_time; i++) {
-      if ((dist = dist_square(num_coords, num_points, points, obj_idx, 
-            &s_clusters[i * num_coords]))
-            < min_dist) {
-          min_dist = dist;
-          new_cluster_idx = i;
+        if ((dist = dist_square(num_coords, num_points, points, obj_idx, 
+              &s_clusters[i * num_coords]))
+              < min_dist) {
+            min_dist = dist;
+            new_cluster_idx = i;
         }
       }
     }
-
-  }
 
 #ifdef DEVICE_TIMING
 if (tid == 0) {

}
#endif

}

inline void checkCudaError(cudaError_t error) {

  int length_per_time = s_per_time * num_coords;
  int times = (int) num_clusters / s_per_time;

  for (obj_idx = bid * bdim + tid; obj_idx < num_points; obj_idx += num_threads) {

    int new_cluster_idx = 0;
    float dist, min_dist = 3.40282e+38;

    // save centroids into shared memory by tiles, and calculate distances
    for (int t = 0; t < times; t ++) {

      for (int i = tid; i < length_per_time; i ++) {
        s_clusters[i] = clusters[t * s_per_time + i];
      }

	    __syncthreads();

#ifdef DEVICE_TIMING
clock_t start;
