Fix logic order when checking for errors