Code for Chapter 4