starting chapter 5, restructuring some of the code for reuse