defer has to be before error checking