Refactored code to streamline writing process for multiple categories