     // stored in an index and it's neighbour.
     thrust::transform(rayPartitions.begin() + leafNodes, rayPartitions.end(),
                       spherePartitions.begin() + leafNodes,
-                      isLeaf.begin(), MortonIsNodeLeaf());
+                      isLeaf.begin(), MortonIsNodeLeaf(mortonCodes));
     // cout << "isLeaf:\n" << isLeaf << endl;    
 
     static thrust::device_vector<unsigned int> leafNodeIndices(nActiveNodes+1);

    // Compute bounds of geometry partitions and do coarse level ray elimination
    // before intersection.

    // cout << "\n *** GORELESS VICTORY ***\n" << endl;
}


struct MortonIsNodeLeaf {
    // TODO take morton codes into account. If rayPartition.x and
    // rayPartition.y-1 points to the same morton code, then we can't possibly
    // subdivide further.

    __host__ __device__
    bool operator()(const uint2 rayPartition, const uint2 spherePartition) const {
        const float rayCount = (float)(rayPartition.y - rayPartition.x);
        const float sphereCount = (float)(spherePartition.y - spherePartition.x);
        
        //return rayCount * sphereCount <= 16.0f * (rayCount + sphereCount);
        return rayCount * sphereCount <= 11.0f * (rayCount + sphereCount);
    }
};


        : bound(bound), boundInvSize(bound.InvertedSize()) {}

    __host__ __device__
    inline MortonCode operator()(const thrust::tuple<float4, float4> ray) const {

        float3 origin = make_float3(thrust::get<0>(ray));
        const float3 direction = make_float3(thrust::get<1>(ray));
        return (*this)(origin, direction);
    }
    
    __host__ __device__
    inline MortonCode operator()(float3 origin, const float3 direction) const {
        
        float tHit;
        if (!bound.ClosestIntersection(origin, direction, tHit)) 
