extract defer to a function