implemented atexit clean up function