various fixs and refactorings