code for Chapter 2