Tidied up code with minor refactor