refactor max and min