Made the collision detection function more efficient