legend and popup refactor