Tests refactoring, tests adding, some logic refactoring