         "A valid machineId and alwaysOn state required."
 
     JMachine = require './machine'
-    userPlan = getUserPlan account
-
-    @fetchUsage client, options, (err, usage)->
-
-      return callback err  if err?
-
-      if alwaysOn and usage.alwaysOn >= userPlan.alwaysOn
-        return callback new KodingError \
-          """Total limit of #{userPlan.alwaysOn}
-             always on vm limit has been reached.""", "UsageLimitReached"
-
-      { ObjectId } = require 'bongo'
-
-      selector  =
-        $or     : [
-          { _id : ObjectId machineId }
-          { uid : machineId }
-        ]
-        users   : $elemMatch: id: user.getId()
-        groups  : $elemMatch: id: group.getId()
-
-      JMachine.one selector, (err, machine)->
-
-        if err? or not machine?
-          err ?= new KodingError "Machine object not found."
-          return callback err
-
-
-        machine.update
-
-          $set: "meta.alwaysOn": alwaysOn
-
-        , (err)-> callback err
+
+    @fetchUserPlan client, (err, userPlan)=>
+
+      return callback err  if err?
+
+      @fetchUsage client, options, (err, usage)->
+
+        return callback err  if err?
+
+        if alwaysOn and usage.alwaysOn >= userPlan.alwaysOn
+          return callback new KodingError \
+            """Total limit of #{userPlan.alwaysOn}
+               always on vm limit has been reached.""", "UsageLimitReached"
+
+        { ObjectId } = require 'bongo'
+
+        selector  =
+          $or     : [
+            { _id : ObjectId machineId }
+            { uid : machineId }
+          ]
+          users   : $elemMatch: id: user.getId()
+          groups  : $elemMatch: id: group.getId()
+
+        JMachine.one selector, (err, machine)->
+
+          if err? or not machine?
+            err ?= new KodingError "Machine object not found."
+            return callback err
+
+
+          machine.update
+
+            $set: "meta.alwaysOn": alwaysOn
+
+          , (err)-> callback err
 
 
   @fetchUsage = (client, options, callback)->

    callback null, "Koding is the best #{ client.r.account.profile.nickname }!"


  getUserPlan = (account)->

    knownPlans = Object.keys PLANS
    flags = account.globalFlags or []

    PLANS[ do ->

      for plan in knownPlans
        return plan  if "plan-#{plan}" in flags

      return 'free'

    ]


  checkUsage = (usage, plan, storage)->

    { r: { group, user, account } } = client

    storage ?= 3

    @fetchUserPlan client, (err, userPlan)=>

      return callback err  if err?

      @fetchUsage client, options, (err, usage)->

        return callback err  if err?

        if err = checkUsage usage, userPlan, storage
          return callback err

        guessNextLabel user, group, label, (err, label)->

          meta =
            type          : "amazon"
            region        : "us-east-1"
            source_ami    : "ami-2651904e"
            instance_type : "t2.micro"
            storage_size  : storage
            alwaysOn      : no

          callback null, {
            meta, label, credential: client.r.user.username
          }


  @update = (client, options, callback)->
