Lots of minor bug fixes and improvements.
