Final code for chapter 1