Tidied up version of the code.