Further tweaks and bug fixes, and code cleanup.

Bug: 8530194
Bug: 8531032
Change-Id: I908e7df4a432f9f6b338bc601e7be08f26b93b98
17 files changed