Add ReplaceSeries and refactor the CreateSeries function into smaller steps

Done. I've created a function that is called CanonicalPublicKeyOrder which does the sorting and promises to not change the input. I use that here as well as in the code.