Remove some redundant NULL checks and fix a free on error return.