refactored problem 21