Extract method compare_index to eliminate ugly conditional