lots of fixes and improvements