cleanup and remove anonymous closures around defer