refactored collision function