extensive refactoring, stability improvements and additional tests