added sortLegend, fixed dispalybugs