reorder the if's for clarity