Refactored code to prepare for category iteration