Clean up sort logic