Various refactorings and fixes.