Test fixes.
Various refactorings.