refactorings and bugfixes