More fixes and refactorings