Code for exercise 2