Pulled CSV export functionality out of ActiveRecord to speed it up.