bugfix and refactor