                           const unsigned int nOldLeafs, // number of leafs prior to checking for new ones.
                           const unsigned int nNewLeafs, // number of new leafs
                           // Results
-                          unsigned int* leafIndices,
-                          unsigned int* leafOwners,
                           unsigned int* nextIndices,
                           unsigned int* nextOwners) {
 
-    const unsigned int id = threadIdx.x + blockDim.x * blockIdx.x;
+    unsigned int id = threadIdx.x + blockDim.x * blockIdx.x;
     if (id >= nActiveIndices) return;
+    
+    id += nOldLeafIndices;
 
     // Owners are global, so it's minus the number of leaves to get the owner of
     // the active indices.
     const unsigned int owner = owners[id] - nOldLeafs;
-    const uint2 partition = nodePartitions[owner];
+    const uint2 partitioning = nodePartitions[owner];
     
     const bool leaf = isLeaf[owner];
 
     // Compute the new index of the data
     const unsigned int partitioningBegin = leaf ? 
-        leafStartIndex[owner] :
-        partition.x - nNewLeafIndices;
-    // Since id's index into active data and partitions contain global indices,
-    // we need to add nOldLeafIndices.
-    const unsigned int partitionOffset = id + nOldLeafIndices - partition.x;
-    const unsigned int dataIndex = partitioningBegin + partitionOffset;
-    
-    unsigned int* newIndices = leaf ? leafIndices : nextIndices;
-    newIndices[dataIndex] = indices[id];
+        leafSphereBeginEntries[owner] + nOldLeafIndices: 
+        partitioning.x + nNewLeafIndices;
+
+    // Offset of the data in the partitioning relative to the beginning.
+    const unsigned int offset = id - partitioning.x;
+    const unsigned int newEntry = partitioningBegin + offset;
+    
+    nextIndices[newEntry] = indices[id];
 
     // Compute the new owner of the data. If it has been moved to a leaf node,
     // then the new owner is the addition of the number of old leafs and the

    nextRayPartitions.resize(rayPartitions.size());
    nextSpherePartitions.resize(spherePartitions.size());
    
    cudaFuncGetAttributes(&funcAttr, PartitionNewLeafNodes);
    blocksize = funcAttr.maxThreadsPerBlock > 256 ? 256 : funcAttr.maxThreadsPerBlock;
    blocks = (nActiveNodes / blocksize) + 1;
    PartitionNewLeafNodes<<<blocks, blocksize>>>
        (RawPointer(rayPartitions) + leafNodes,
         RawPointer(spherePartitions) + leafNodes,
         RawPointer(isLeaf),

    thrust::transform(rayPartitions.begin() + leafNodes, rayPartitions.end(),
                      spherePartitions.begin() + leafNodes,
                      isLeaf.begin(), IsNodeLeaf());
    cout << "isLeaf:\n" << isLeaf << endl;    

    static thrust::device_vector<unsigned int> leafNodeIndices(nActiveNodes+1);
