Add new tests and refactor