Rearranged the exception handling to make more sense.