Tidy up codes for chapter 1 and chapter 2