Improve trim_dict efficiency and basic test