Refactoring & bugfixes & more tests added