     console.log(result)
     single_defer.resolve()
   )
-  group_encryption(form, values, (result) ->
-    #console.log(result)
-    group_defer.resolve()
-  )
-  $.when(single_defer, group_defer).done(() -> # Spat defers to param list with apply
+  $.when(single_defer).done(()->
+    group_encryption(form, values, (result) ->
+      console.log(result)
+      group_defer.resolve()
+    )
+  )
+  $.when(single_defer, group_defer).done(() ->
+    #console.log values
+    #reinsert_values values
     #form.submit()
   )
 

  buffer = collect_sources(elements)
  encryption_source_names = find_names_for(form, elements)
  wipe_source_fields(values, encryption_source_names)
  #encrypt buffer
  #write to target
  callback()

#  write_target(buffer)
#elements_with_name_in = (source, reference) ->
#  return $.grep(source, (element) ->
#    contains(element.name, reference)
#  )

collect_sources = (elements)->
  buffer = ""

    if contains(element.name, names_to_encrypt)
      d = $.Deferred()
      defers.push d
      encrypt(element.value, (result_string)->
        element.value = result_string.toString()
        console.log "#{element.name} finished encryption"
