fixed calculation of maximum value