Lots of bug fixes and improvements