   searchProcess = fork(searchTask, env)
 
   searchProcess.stdin.setEncoding = 'utf-8';
-  scanProcess.stdout.on 'data', (data) ->
-    searchProcess.stdin.write(data)
+  scanProcess.stdout.pipe(split()).on 'data', (data) ->
+    if data[0] == STOP_CHAR
+      scanFinished = true
+      console.log 'Scan done'
+      maybeEnd()
+    else
+      searches++
+      searchProcess.stdin.write(data+'\n')
 
   searchProcess.stderr.pipe(process.stderr)
 
   searchProcess.stdout.pipe(split()).on 'data', (line) ->
     if line[0] == START_CHAR
       console.log "search start"
-      searches++
     else if line[0] == END_CHAR
       searches--
       console.log "search end #{searches}"
     else if line and line.length
-      results = JSON.parse(line)
+      try
+        results = JSON.parse(line)
+      catch e
+        console.log "AHH", e
       if results
         pathCount++
         resultCount += results.results.length


  emitPaths = (paths) ->
    process.stdout.write(JSON.stringify(paths)+'\n')
  done = ->
    process.stdout.write(STOP_CHAR+'\n')
    process.send(event: 'finished')

  options =
    pathToScan: process.env.pathToScan


  START_CHAR = String.fromCharCode(1)
  END_CHAR = String.fromCharCode(4)
  STOP_CHAR = String.fromCharCode(0)

  scanTask = makeTask(multiProcessScan)
  searchTask = makeTask(multiProcessSearch)
