refactored collision detection