bugfix and tests
refactor & bugfix