custom function to identify min and max