algorithm was relying on dict items to be sorted!