Finished up the Exercise 1 function

Perhaps the pattern matches are a bit verbose and could be optimised
further.