code cleanup and legend support