-- some refactoring for writing to file and csv