Several fixes and improvements