Added write to CSV functionality