Fix sorting crashing bug