Added docstrings, did minor tidy-ups.