Exercise 2.7 from the book finished