 do ->
 
-  loadWorkspace = (slug) ->
-    workspace = ws  for ws in KD.userWorkspaces when ws.slug is slug
+  loadWorkspace = (machineLabel, workspaceSlug) ->
+    workspace = ws  for ws in KD.userWorkspaces when ws.slug is workspaceSlug
+    machine   = getMachineByLabel machineLabel
 
     if workspace
-      machine = getMachineByUId workspace.machineUId
-      loadIDE { machine, workspace } # TODO: Be sure we are handling machine not found
+      loadIDE { machine, workspace }
     else
-      if slug is 'my-workspace'
-        machine   = KD.userMachines.first
-        workspace = isDefault: yes, slug: 'my-workspace'
-
+      if workspaceSlug is 'my-workspace'
+        workspace = isDefault: yes, slug: 'my-workspace', machineLabel: machine.label
         loadIDE { machine, workspace }
       else
-        routeToLastWorkspace()
+        routeToLatestWorkspace()
 
 
   selectWorkspaceOnSidebar = (data) ->

      fallback()


  routeToLastWorkspace = ->
    KD.getSingleton('router').handleRoute '/IDE/my-workspace'


  KD.registerRoutes 'IDE',

    '/:name?/IDE': -> routeToLastWorkspace()

    '/:name?/IDE/:workspaceSlug': (data) ->
      loadWorkspace data.params.workspaceSlug

      isSameMachine   = instance.mountedMachineUId is machineUId
      isSameWorkspace = instance.workspaceData is workspace

      if isSameMachine
        if isSameWorkspace then ideInstance = instance
        else if workspace.slug is 'my-workspace'
          if instance.workspaceData?.isDefault
            ideInstance = instance

    if ideInstance
      appManager.showInstance ideInstance
