Refactored problem 1.