Add multi dimension sorting and clarify some variable naming.