replaced min/max functions to MIN/MAX