Tidied up and refactored old code