minimize the use of min & max function