optimize the legends