Refactored collision detection