Adding docstrings, misc small restructurings.