improve api to receive a before function