rearranged if logic