A batch of bug fixes (for the refactor branch).

_These are all refactor branch bugs. There might be a couple of very minor bugs that exist on the master too._

I'm not going to list all the fixes unless you want me to.