Added collision detection logic