Collision detection function added