         /******       APPROX          ******/
         /***********************************/
 
-        n_total = calc_prefix(prefix, 1 - (split || !use_node));
-        offset = prefix[laneId];
-
-        if (!split && use_node) approxM[n_approx + offset - 1] = node;
+        /* binary prefix sum */
+        flag = !split && use_node;
+        n_total = warp_exclusive_scan(flag, offset);
+        if (flag) approxM[n_approx + offset] = node;
+
         n_approx += n_total;
 
         while (n_approx >= DIM) 

        body_list[laneId] = directM[laneId];                                            //copy list of bodies from previous pass to body_list

        // step 1
        calc_prefix(prefix, flag);                                // inclusive scan on flags to construct array
        const int offset1 = prefix[laneId];

        // step 2
        int n_bodies  = calc_prefix(prefix, nbody);              // inclusive scan to compute memory offset for each body
        offset = prefix[laneId];

        if (flag) prefix[offset1 - 1] = laneId;                             //with tidś whose leaves have to be opened

        directM[laneId]  = offset;                                       //Store a copy of inclusive scan in direct
        offset       -= nbody;                                              //convert inclusive int oexclusive scan

  return mysum;
}

/* inclusive prefix sum for a warp */
__device__ __forceinline__ int inclusive_scan_warp(int* prefix, int value) 
{
  prefix[laneId] = inclusive_scan_warp<WARP_SIZE2>(value);
  return prefix[WARP_SIZE-1];
}


/* inclusive prefix sum for an array */
__device__ int inclusive_scan_array(int N, int* prefix_in) 
{

  int y = inclusive_scan_warp(prefix_in, prefix_in[laneId]);
  if (N <= WARP_SIZE) return y;

  for (int p = WARP_SIZE; p < N; p += WARP_SIZE) 
  {
    int *prefix = &prefix_in[p];
    const int y1 = inclusive_scan_warp(prefix, prefix[laneId]);
    prefix[laneId] += y;
    y += y1;
  }

  return y;
} 

/**** binary scans ****/

__device__ __forceinline__ int lanemask_lt()
{
  int mask;
  asm("mov.u32 %0, %lanemask_lt;" : "=r" (mask));
  return mask;
}


__device__ int warp_exclusive_scan(const bool p, int &psum)
{
  const unsigned int b = __ballot(p);
  psum = __popc(b & lanemask_lt());
  return __popc(b);
}

__device__ int warp_exclusive_scan(const bool p)
{
  const int b = __ballot(p);
  return __popc(b & lanemask_lt());
}

/************************************/
/********* SEGMENTED SCAN ***********/
