       else
         varNames.push vars...
       {conds, preassigns} = constructCond precs
-      hoistedWheres = hoistWheres hoistableWheres, assigns
+      [hoistedWheres, furtherHoistable] = hoistWheres hoistableWheres, assigns
       #log "assigns", printAst assigns
       """#{control} (#{conds}) {
            #{preassigns.concat(assigns).map(compileAssign).join '\n'}
-           #{hoistedWheres}
-           return #{compileImpl result};
+           #{hoistedWheres.map(compileDef).join '\n'}
+           return #{compileImpl result, furtherHoistable};
         }"""
       )
     mainCache ?= []

hoistWheres = (possible, assigns) ->
  defined = (n for [n, _] in assigns)
  hoisted = []
  for [pattern, def, names] in possible
    canHoist = yes
    for name in names when name not in defined
      canHoist = no
    if canHoist
      hoisted.push [pattern, def]
  if hoisted.length > 0
    compileWhereImpl hoisted
  else
    ''

toJsString = (token) ->
  "'#{token}'"

  ]

graphToWheres = (graph) ->
  graph.map ({def: [pattern, def], missing}) -> [pattern, def, missing]

# Returns two new graphs, one which needs hoisting and one which doesnt
findHoistableWheres = ([graph, lookupTable]) ->
