Refactored for simplicity, clarity and dependency injection.