         __syncthreads();                                                 //Thread barrier to make sure all warps finished writing data
 
         bool flag = (split && !leaf) && use_node;                        //Flag = use_node + split + not_a_leaf;Use only non_leaf nodes that are to be split
-#if 0
-        if (flag) nodesM[prefix[tid]] = child;                            //Thread with the node that is about to be split
-        __syncthreads();                                                 //writes the first child in the array of nodes
-
+        if (flag) nodesM[offset] = child;                            //Thread with the node that is about to be split
+                                                                     //writes the first child in the array of nodes
         /*** 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 && nodesM[prefix[tid] + 1] == 0) nodesM[prefix[tid] + 1] = child + 1; __syncthreads();
-        if (flag && nodesM[prefix[tid] + 2] == 0) nodesM[prefix[tid] + 2] = child + 2; __syncthreads();
-        if (flag && nodesM[prefix[tid] + 3] == 0) nodesM[prefix[tid] + 3] = child + 3; __syncthreads();
-        if (flag && nodesM[prefix[tid] + 4] == 0) nodesM[prefix[tid] + 4] = child + 4; __syncthreads();
-        if (flag && nodesM[prefix[tid] + 5] == 0) nodesM[prefix[tid] + 5] = child + 5; __syncthreads();
-        if (flag && nodesM[prefix[tid] + 6] == 0) nodesM[prefix[tid] + 6] = child + 6; __syncthreads();
-        if (flag && nodesM[prefix[tid] + 7] == 0) nodesM[prefix[tid] + 7] = child + 7; __syncthreads();
-#else
-#if 1
-        if (flag) nodesM[offset] = child;                            //Thread with the node that is about to be split
-        //writes the first child in the array of nodes
-#else
-        const int maskT = flag ? 0xFFFFFFFF : 0x0;
-        const int maskF = ~maskT;
-        const int addr = (offset & maskT) + (-1 & maskF);;
-        nodesM[addr] = (maskF & nodesM[addr]) + (maskT & child);
-#endif
-
         if (flag && nodesM[offset + 1] == 0) nodesM[offset + 1] = child + 1; 
         if (flag && nodesM[offset + 2] == 0) nodesM[offset + 2] = child + 2;
         if (flag && nodesM[offset + 3] == 0) nodesM[offset + 3] = child + 3;

**** --> prefix calculation via Horn(2005) data-parallel algoritm
***/
#define BTEST(x) (-(int)(x))
template<int DIM2>
__device__ int calc_prefix(int N, int* prefix_in, int tid) {
  int x, y = 0;

  const int DIM = 1 << DIM2;
  
  for (int p = 0; p < N; p += DIM) {
    int *prefix = &prefix_in[p];

    x = prefix[tid -  1]; __syncthreads(); prefix[tid] += x & BTEST(tid >=  1); __syncthreads();
    x = prefix[tid -  2]; __syncthreads(); prefix[tid] += x & BTEST(tid >=  2); __syncthreads();
    x = prefix[tid -  4]; __syncthreads(); prefix[tid] += x & BTEST(tid >=  4); __syncthreads();
    x = prefix[tid -  8]; __syncthreads(); prefix[tid] += x & BTEST(tid >=  8); __syncthreads();
    x = prefix[tid - 16]; __syncthreads(); prefix[tid] += x & BTEST(tid >= 16); __syncthreads();
    if (DIM2 >= 6) {x = prefix[tid - 32]; __syncthreads(); prefix[tid] += x & BTEST(tid >= 32); __syncthreads();}
    if (DIM2 >= 7) {x = prefix[tid - 64]; __syncthreads(); prefix[tid] += x & BTEST(tid >= 64); __syncthreads();}
    if (DIM2 >= 8) {x = prefix[tid -128]; __syncthreads(); prefix[tid] += x & BTEST(tid >=128); __syncthreads();}
    

    prefix[tid] += y;
    __syncthreads();

    y = prefix[DIM-1];
    __syncthreads();
  }

  return y;
} 

template<int DIM2>
__device__ int calc_prefix1(int* prefix, int tid, int value)
{
  int  x;

    return x;
  }
}


template<int DIM2>
__device__ int calc_prefix(int N, int* prefix_in, int tid) 
{
  const int DIM = 1 << DIM2;
  const int N2  = N >> DIM2;

  int y = calc_prefix<DIM2>(prefix_in, tid, prefix_in[tid]);
  if (N2 == 0) return y;

  for (int p = DIM; p < N; p += DIM) 
  {
    int *prefix = &prefix_in[p];
    const int y1 = calc_prefix<DIM2>(prefix, tid, prefix[tid]);
    prefix[tid] += y;
    y += y1;
  }
  __syncthreads();

  return y;
} 



  template<int SHIFT>
