moved file writing to a separate thread