bugfix and refactoring