Refactor distinct method