Refactor, add duplicate check, tests.