final version cleaned up for problem 22