start some refactorings in legend.py