Improved performance slightly and made an legend option to group duplicate keys