Improved code in chapter 1, working on chapter 3.