Tidied up the code for better reading