Problem 119, refactoring of problem 73.