extracts write-core-file code to a function