Fix broken code that assumes dict keys are in sorted order