Added docstrings, plus some minor code cleanup.