Tidied up a bit - working code but about to attempt to refactor