Refactored the collision detection