Code for chapter 4