var _ = require('underscore'),
  fs = require('fs');

function Writer() {
  
  function gen(templates, numSegments, params, outFile, cb) {
    var stream = fs.createWriteStream(outFile,
        { flags: 'w', encoding: 'utf-8' }),
      segmentId = 0,
      segment;

    stream.on('error', function (err) {
      console.error('Error: ' + err);
    });

    stream.on('close', function () {
      cb();
    });

    stream.write(templates.header);

    while (segmentId++ < numSegments) {
      segment = templates.segment;
      params.segment_id = segmentId;
      
      function _apply(param) {
        segment = segment.replace(new RegExp('{' + param + '}', 'g'), params[param]);
      }
      
      _.keys(params).forEach(_apply);
      stream.write(segment);
    }

    stream.end(templates.footer);
  }

  return {
    gen: gen
  };
}

exports.Writer = Writer;
@@ -246,25 +246,10 @@
  = 0
-,%0A      segment
 ;%0A
+
 %0A   
@@ -435,40 +435,40 @@
     
-while (segmentId++ %3C numS
+function _writeSegment(s
 egment
-s
+Id
 ) %7B%0A
@@ -469,24 +469,28 @@
 Id) %7B%0A      
+var 
 segment = te
@@ -720,16 +720,16 @@
 apply);%0A
-
       st
@@ -756,16 +756,95 @@
 %0A    %7D%0A%0A
+    while (segmentId++ %3C numSegments) %7B%0A      _writeSegment(segmentId);%0A    %7D%0A%0A
     stre

Clean up lint violations.