adding code from chapter 5