Refactored code of comparator