Listing 9.3 with all tests passing as expected.