sort dict, fix bugs