Final code for exercise 9