improve the "expand_functions" function