More tests and refactor