Refactor isPalindrome.