Code cleanup and test fixes.