Add tests and refactor helper