Tidied up and documented properly.