Refactored for better testing