Updated code for chapter 7