enhancing algorithm to allow callbacks to refer to any local variables.