Minor cleanup - added some docstrings, renamed some variables to make everything clearer.