some bugfixes and refactoring