Fixed the output function so that it writes to csv corrctly.