More fixes and refactoring