Code for chapter 4 (with error!)