more bugfixing/refactoring