several refactorings and bugfixes