   spawnProcess: (rootDir,options)=>
     return unless @processMap?
     return unless atom.config.get('haskell-ghc-mod.enableGhcModi')
-    timer = setTimeout (=> @killProcessForDir rootDir), 60*60*1000
+    timer = setTimeout (=>
+      debug "Killing ghc-modi for #{rootDir.getPath()} due to inactivity"
+      @killProcessForDir rootDir), 60*60*1000
     proc = @processMap.get(rootDir)
+    debug "Checking for ghc-modi in #{rootDir.getPath()}"
     if proc?
+      debug "Found running ghc-modi instance for #{rootDir.getPath()}"
       clearTimeout proc.timer
       proc.timer = timer
       return proc.process
+    debug "Spawning new ghc-modi instance for #{rootDir.getPath()} with
+          #{"options.#{k} = #{v}" for k,v of options}"
     modiPath = atom.config.get('haskell-ghc-mod.ghcModiPath')
     proc = CP.spawn(modiPath,[],options)
     proc.on 'stderr', (data) ->
       console.error 'Ghc-modi says:',data
     proc.on 'exit', (code) =>
+      debug "ghc-modi for #{rootDir.getPath()} ended with #{code}"
       @processMap.delete(rootDir)
       @spawnProcess(rootDir,options) if code!=0
     @processMap.set rootDir,

      cmd = [command, uri].concat args
    else
      cmd = [command].concat args
    process.stdin.write cmd.join(' ').replace(/\r|\r?\n/g,' ') + '\n'

    if text?
      process.stdin.write "unload #{uri}\n"

  killProcess: =>
    return unless @processMap?
    atom.project.getDirectories().forEach (dir) =>
      @killProcessForDir dir

  killProcessForDir: (dir) =>
    return unless @processMap?
    clearTimeout @processMap.get(dir)?.timer
    @processMap.get(dir)?.process.stdin?.end?()
    @processMap.get(dir)?.process.kill?()

          callback result.slice(0,-1).map (line)->
            line.replace /\0/g,'\n'
    if text?
      debug "sending stdin text to #{modPath}"
      process.process.stdin.write "#{text}\x04\n"

  runModiCmd: ({dir,options,command,text,uri,args,callback}) =>
    debug "Trying to run ghc-modi in #{dir.getPath()}"
    process=@spawnProcess(dir,options)
    unless process
      debug "Failed. Falling back to ghc-mod"
      return @runModCmd {options,command,text,uri,args,callback}
    parseData = (data)->
      debug "Got response from ghc-modi:\n#{data}"
      lines = "#{data}".split("\n")
      result = lines[lines.length-2]
      unless result.match(/^OK/)
