Added sorting by public key in bunch of places to conform to the protocol