Several fixes and improvements.

Several fixes and improvements.