refactored code to prepare for future changes and manage collision