Misc bug fixes, style changes and improvements.