Fix some issues with sorting