Remove unneeded functions and corresponding terms.