         sh_body  [tid] = jbody;                                                  //store the leafs first body id into shared memory
 
         // step 1
-#ifdef OLDPREFIX
-        calc_prefix<DIM2>(prefix0, tid, flag);
-#else
-#if 0
-        inclusive_scan_block<ADDOP<int>, int, DIM2>(prefix0, (int)flag, tid);       // inclusive scan on flags to construct array
-#else
-        v0 = inclusive_scan_block<ADDOP<int>, int, DIM2>((int)flag, tid);       // inclusive scan on flags to construct array
+#if 0
+        int v0 = inclusive_scan_block<DIM2>(prefix_shmem, (int)flag, tid);       // inclusive scan on flags to construct array
         prefix0[tid] = v0;
         __syncthreads();
-#endif
-#endif
 
         if (flag) prefix1[prefix0[tid] - 1] = tid;                             //with tidś whose leaves have to be opened
         __syncthreads();                                                      //thread barrier, make sure all warps completed the job
+#else
+        offset = inclusive_scan_block<DIM2>(prefix_shmem, (int)flag, tid);       // inclusive scan on flags to construct array
+        if (flag) prefix1[offset - 1] = tid;                             //with tidś whose leaves have to be opened
+#endif
 
         // step 2
-#ifdef OLDPREFIX
-        int n_bodies  = calc_prefix<DIM2>(prefix0, tid, nbody);
-#else
-#if 0
-        inclusive_scan_block<ADDOP<int>, int, DIM2>(prefix0, nbody, tid);        // inclusive scan to compute memory offset for each body
-#else
-        v0 = inclusive_scan_block<ADDOP<int>, int, DIM2>(nbody, tid);        // inclusive scan to compute memory offset for each body
-        prefix[tid] = v0;
+#if 0
+        int v0 = inclusive_scan_block<DIM2>(prefix_shmem, nbody, tid);        // inclusive scan to compute memory offset for each body
+        prefix0[tid] = v0;
         __syncthreads();
-#endif
         int n_bodies = prefix0[blockDim.x - 1];                            //Total number of bides extract from the leaves
         __syncthreads();                                                   // thread barrier to make sure that warps completed their jobs
-#endif
 
         direct [tid]  = prefix0[tid];                                       //Store a copy of inclusive scan in direct
         prefix0[tid] -= nbody;                                              //convert inclusive int oexclusive scan
         prefix0[tid] += 1;                                                  //add unity, since later prefix0[tid] == 0 used to check barrier
+#else
+        offset2 = inclusive_scan_blockS<DIM2>(prefix_shmem, nbody, tid);        // inclusive scan to compute memory offset for each body
+        int offset1  = offset2.x;
+        int n_bodies = offset2.y;
+
+        direct[tid] = offset1;
+        offset1    -= nbody;
+        offset1    += 1;
+        prefix0[tid] = offset1;
+        __syncthreads();
+#endif
 
         int nl_pre = 0;                                                     //Number of leaves that have already been processed
 



#define TEXTURES
// #define OLDPREFIX


template<int DIM2, int SHIFT>

        if (flag && nodes[prefix[tid] + 5] == 0) nodes[prefix[tid] + 5] = child + 5; __syncthreads();
        if (flag && nodes[prefix[tid] + 6] == 0) nodes[prefix[tid] + 6] = child + 6; __syncthreads();
        if (flag && nodes[prefix[tid] + 7] == 0) nodes[prefix[tid] + 7] = child + 7; __syncthreads();
#else
        /* Thread with the node that is about to be split */
        if (flag                          ) nodes[offset    ] = child;    
        /* in the following 8 lines, we calculate indexes of all the children that have to be walked from the index of the first child */
        if (flag && nodes[offset + 1] == 0) nodes[offset + 1] = child + 1;
        if (flag && nodes[offset + 2] == 0) nodes[offset + 2] = child + 2;
        if (flag && nodes[offset + 3] == 0) nodes[offset + 3] = child + 3;
        if (flag && nodes[offset + 4] == 0) nodes[offset + 4] = child + 4;
        if (flag && nodes[offset + 5] == 0) nodes[offset + 5] = child + 5;
        if (flag && nodes[offset + 6] == 0) nodes[offset + 6] = child + 6;
        if (flag && nodes[offset + 7] == 0) nodes[offset + 7] = child + 7;
        __syncthreads();
#endif

        n_offset += n_total;    //Increase the offset in the array by the number of newly added nodes

