Better function to work out exercise 2