Added extra boundrary checking for collision.