Code cleanup

* fix off-by-one error
* docstrings
* pep8 fixes