Fixed comparators, refactored some functions to keep DRY