Numerous fixes and improvements