Refactoring and adding uniq by condition