move CSV building logic out of the controller