finished max function