Lots of bugfixes, and other improvements