Bug fixes and minor code cleanup.