Refactored for better organization.