rewrite defer func