Removes duplicate code for creating csv