Code for Part 4 of the tutorial