bugfix & some refactoring