refactored problem 3