refactoring and small bugfix