moved reference counting out of DynamicFunction