A few bug fixes and cosmetic code changes.

Change-Id: Iaeed8ab7ba3323463ab7d3271c870605f5cb4640