refactored problem 2 solution