Problem 3 solution and minor cleanup.