Tidied up the code for easier reading