Problem 5 solution and some cleanup.