converted write_to_file to accept block