Refactored problem 2