Writing to file from background thread