some refactoring + bugfixes