More fixes and refactorings.