[Refactor] Optimized 3D logic for performance