bug fix, some refactoring, tests