Several bug fixes and improvements.