implemented cleanup function