Optimized the collision detection