Tidied up code, ready to submit