cleaned up solution to Problem 8