tidied up code and corrected docs.