refactoring and bugfixes