Updated in response to reviewer feedback

* renamed functions
* added lots of comments
* some minor refactoring
* moved all code into one file

Also, some unrelated bugs have been fixed.