refactor problem 5 solution