Refactored some code to suit the chapter