refactored problem 8