Problem 4 solution and minor cleanups.