Updating and improving a bunch of things

Major pull-request.  
Among other things:

* fixing #227
* fixing #226
* fixing #224
* fixing #221
* partially fixing #220
* working around #218
* improving overall code quality
* adding comments

**Merge with caution!** I didn't test the code. Please make sure you test it before you merge it. :)