refactoring and bugfix