Code for chapter 5.