Finished solution for exercise 4.