Add a function factory and some unit tests