refactored problem 4