Cleaned up some bits of code; added docstrings to all functions.