Various fixes and improvements.

Change-Id: Ib794de1d39f22b8feaeabfbe64e539cc6ae4f0fc