 		}
 
 		if (!traverseLeft && !traverseRight) {
+			if (nestedStackPos == stackpos) {
+				nestedStackPos = -1;
+				isNested = false;
+			}
+
 			node = *(--stack);
 			stackpos -= 1;
 		}

			if (traverseLeft && traverseRight) {
				*(stack++) = right;
				stackpos += 1;
			}
		}
		AT_ASSERT(0 <= stackpos && stackpos < STACK_SIZE);

	*stack++ = nullptr;

	int stackpos = 1;
	int nestedStackPos = -1;

	aten::BVHNode* node = &ctxt.nodes[0];

	aten::ray transformedRay = r;
	bool isNested = false;

	do {
		aten::BVHNode* left = node->left >= 0 ? &ctxt.nodes[node->left] : nullptr;
