Fix memory leak in Function