numerous bug fixes and improvements