Move an error checking to the if block where it belongs