Bunch of tests and refactoring