         return -1;
         }
     
-    unsigned int first_code = d_morton_codes[i];
-    unsigned int last_code = d_morton_codes[j];
+    uint32_t first_code = d_morton_codes[i];
+    uint32_t last_code = d_morton_codes[j];
     
     // if codes match, then use index as tie breaker
+    // the number of shared bits is equal to the 32 bits in the integer, plus the number of bits shared between the
+    // indexes (offset from the start of the node range to make things simpler)
     if (first_code == last_code)
         {
-        return __clz(i ^ j);
+        return (32 + __clz((i-min_idx) ^ (j-min_idx)));
         }
     else
         {

 *       Tero Karras, "Maximizing parallelism in the construction of BVHs, octrees, and k-d trees", 
 *       High Performance Graphics (2012).
 */
__device__ inline uint2 determineRange(const unsigned int *d_morton_codes,
                                       const int min_idx,
                                       const int max_idx,
                                       const int idx)

 * \returns the leaf index corresponding to the split in Morton codes
 * See determineRange for original source of algorithm.
 */
__device__ inline unsigned int findSplit(const uint32_t *d_morton_codes,
                                         const unsigned int first,
                                         const unsigned int last)
    {
    uint32_t first_code = d_morton_codes[first];
    uint32_t last_code = d_morton_codes[last];
    
    // if codes match, then just split evenly
    if (first_code == last_code)
