freeing memory and files when done with them