Miscellaneous fixes and code cleanup.