Added write out to csv functionality