fixed cleanup function