cleaned up code for problem 12