cleanup and legend filtering