Tidied up messy code