properly free memory when exit