Moved CSV creation to a procedure