Minor performance tweaks with sorting