         type            : Boolean
         default         : no
 
-  @createDomains = (domains, hostnameAlias) ->
+  @createDomains = (account, domains, hostnameAlias)->
     JDomain = require './domain'
+    console.log "creating domains for ", account, domains, hostnameAlias
     domains.forEach (domain) ->
-      JDomain.one {domain}, (err, domainData)->
-        return  if domainData or err
-        (new JDomain
-          domain        : domain
-          hostnameAlias : [hostnameAlias]
-          proxy         : { mode: 'vm' }
-          regYears      : 0
-        ).save (err)-> console.log err  if err?
-
-  @ensureDomainSettings = ({vm, type, nickname, groupSlug})->
+      domainObj = new JDomain
+        domain        : domain
+        hostnameAlias : [hostnameAlias]
+        proxy         : { mode: 'vm' }
+        regYears      : 0
+      domainObj.save (err)->
+        console.log err  if err?
+        unless err
+          account.addDomain domainObj, (err)->
+            console.log err  if err?
+
+  @ensureDomainSettings = ({account, vm, type, nickname, groupSlug})->
     domain = 'kd.io'
     if type is 'user'
       requiredDomains = ["#{nickname}.#{groupSlug}.#{domain}"]

    JGroup  = require './group'
    JUser   = require './user'

    addVm = ({ target, user, sudo, groups, groupSlug
               type, planCode, planOwner, webHome })->

      uid = 0

              return console.warn "Failed to create VM for ", \
                                   {users, groups, hostnameAlias}

            JVM.createDomains account, hostnameAliases, hostnameAliases[0]

            group.addVm vm, (err)=>
              return callback err  if err
              JVM.ensureDomainSettings {account, vm, type, nickname, groupSlug}
              if type is 'group'
                @addVmUsers vm, group, ->
                  callback null, vm
