Lots of minor bug fixes