Better handling of max history length in dictionary