fixed max function to throw proper error