         #endif
 
 
-
-//    if (bx+tx < (N/32)*32) { //problem size rounded up to nearest multiple of warp size
-
             int laneid = tx & (32-1);
-
-            for (int j=0; j < 32-laneid; j++) {
+            if (output) {
+            for (int j=0; j < 32-laneid && output; j++) {
                 if (i+j >= 0 && i+j<N) {
 
                 float diffct = ct_i - LDG(ct,i+j);

                float diffy = y_i - LDG(y,i+j);
                float diffz = z_i - LDG(z,i+j);

                if ((diffct * diffct < diffx * diffx + diffy * diffy + diffz * diffz) && output) {
                    #if write_sums == 1
                    sum++;
                    #endif


                }
            }
            } // end of if output

    #if write_sums == 1
    if (bx+tx < N) {
        sums[bx+tx] = sum;
    }
    #endif

    //collaboratively write back the output collected in shared memory to global memory

