Collision detection code