Rearranged the logic