Code for chapter 2