Refactored IsPair function