Problem 18 solution and some cleanup.