some more fixes and refactoring