Factored out addFunction and clearFunction