some bugfixes and refactorization