Finished code for tutorial 4.6.