refactoring solution to problem 77