Refactored problem 3