lots of bugfixes after refactoring