use max instead of sorted