More tests and some refactor