End of chapter 8. Refactored code