refactoring chapter 7 code