Code per Listing 2.4 of the book