Code cleanup and unit test fixes.