Add more tests and some refactorings