Add documentation and allow specification of the handler for the when function through a parameter