Added documentation, a tiny bit of refactoring

Went back and added some signatures, purpose statements, and other
relevant comments. I rewrote a few lines of code as well, but it's still
a complete mess.