Added collision detection logic.