some refactoring and bugfixes