Refactored code for better organization.