refactored max and min