max and min functions not working correctly