Refactor collision functionality to reduce duplication.