Fixed unreliable file descriptor closing, added file cleanup for get_contents_to_filename()