legend cleanup function;