Fixed error in min max calculation