Refactored collision detection.