Further debugging and clean up.
differentiate() modified to prevent infinite looping.