Tidied up some of the code