Cleaner legend generation code.