lots of bug fixes and improvements