Code for solution to question 3