Fix tests, refactoring