small fix in save_to_file and cleanup in insert_into_buffer