Various minor improvements (use `with` blocks, qualify exception types, add comments, etc.)

In general, the code blocks are a little cleaner and easier to understand.  

I also added a few simple comments to help others understand the “big picture” parts of this program.

Overall, I think these changes make the file a little easier to understand for humans, and a little more safe for computers (qualified exceptions, the `with` statement, etc.).