various bug fixes and improvements