tested code against example in textbook