big refactor to avoid a lot of duplicated container sorting and duplicate keys