Bug fixes. code cleanup.