First half of chapter 7 code