Re-factored code for the chapter