Alternative solution to exercise 2