Code for chapter 3