modified to comply with negative values of the functions