Recursive function for comparison