modified so that it can handle functions with limited domain.