Listing 10.8 with all tests passing.