Document program

Add docstrings and/or comments to some of the code with less obvious
behavior.