Lots of minor bugfixes