Refactor and test fixes.