Adding the codes for exercise 4.1