 	*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;

		if (right) {
			if (right->isLeaf()) {
				aten::hitrecord recRight;
				const auto& rightobj = ctxt.shapes[right->shapeid];

				if (intersectShape(rightobj, r, t_min, t_max, recRight)) {
					if (recRight.t < rec.t) {
						rec = recRight;
						rec.obj = (void*)&rightobj;

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

			if (!isNested && nestedStackPos >= 0) {
				const auto& param = ctxt.shapes[node->shapeid];
				transformedRay = param.mtxW2L.applyRay(r);
				isNested = true;
			}
		}
		AT_ASSERT(0 <= stackpos && stackpos < STACK_SIZE);
