Moved collision detection to separate function