Updated with the solution to Programming exercise 2