Refactored collision function