Modified the sort function signature to also pass the original table, so not only keys are available when sorting, but also the values in the original table.
Fixed emitting premature circular reference in self-reference section, which caused invalid serialization.