re-wrote the collision detection for better readability: