rearranged if statements to avoid redundant comparisons