Lots of fixes and improvements