Add function write_to_file to avoid redundancy in writing to file, and modify bootstrap functions accordingly