 
 writeNdjson = (file) ->
 	return (schedules) ->
-		#return console.log schedules   # todo: remove
 		task = Q.defer()
 
 		stringify = ndjson.stringify()
-		file.on 'error', (err) ->
+		stringify.on 'error', (err) ->
 			console.error err.stack   # todo: remove?
 			task.reject err
 
-		file = stringify
-		.pipe fs.createWriteStream path.join __dirname, '../data', file
+		writeStream = fs.createWriteStream path.join __dirname, '../data', file
 
-		file.on 'error', (err) ->
+		writeStream.on 'error', (err) ->
 			console.error err.stack   # todo: remove?
 			task.reject err
-		file.on 'finish', (end) -> task.resolve()
+		writeStream.on 'finish', task.resolve
 
+		stringify.pipe writeStream
 		for id, schedule of schedules
-			return console.log schedule   # todo: remove
+			delete schedule.start
+			delete schedule.days
 			stringify.write schedule
+		stringify.end()
 
 		return task.promise
 

simplify = Q.defer()
schedules = {}

# read & accumulate data
readCsv 'calendar.csv', processSchedule schedules
.then () ->


# pass `schedules` in
.then () ->
	return schedules

# process data
.then computeDaysInSchedules

# write data
.then writeNdjson 'schedules.ndjson'
.catch (err) ->
	console.error err.stack

.then () ->
	console.log 'Done.'
