Fix max length calculation