small bugfixes and refactoring