code cleanup and unit test fixes