               depIndex++
               depsLoaded()
             else
-              root.molo.require i, updateDeps, context
+              if queue[dep]
+                root.molo.require i, updateDeps, context
+              else
+                loadScriptFile appendScriptPath(dep), ->
+                  root.molo.require i, callback, context
         else
-          if isJavaScriptFile(root.molo.paths[name])
-            # If the path is complete (e.g. 'js/lib/mymodule.js') take that as a path
-            scriptPath = root.molo.paths[name]
-          else
-            # Prepend the base path if any
-            prePath = if root.molo.basePath then "#{basePath}" else ''
-            pathArray = Object.keys(root.molo.paths)
-
-            # Walk through all paths and check if the module name starts with the path name
-            for p in pathArray
-              if root.molo.paths[p] and name.indexOf("#{root.molo.paths[p]}#{pathSep}") is 0
-                prePath = root.molo.paths[p]
-
-            scriptPath = if prePath then "#{prePath}#{pathSep}#{i}.js" else "#{i}.js"
-        
-          loadScriptFile scriptPath, ->
-            #root.molo.require i, callback, context
+          loadScriptFile appendScriptPath(i), ->
+            root.molo.require i, callback, context
         
         
   # Additional export functions


isJavaScriptFile = (filename) -> hasExtension filename, '.js'


# Wrapper function
do (root = module?.exports ? this) ->

      if require
        require filename
        callback()

  appendScriptPath = (name) ->
    if isJavaScriptFile(root.molo.paths[name])
      # If the path is complete (e.g. 'js/lib/mymodule.js') take that as a path
      scriptPath = root.molo.paths[name]
    else
      # Prepend the base path if any
      prePath = if root.molo.basePath then "#{basePath}" else ''
      pathArray = Object.keys(root.molo.paths)

      # Walk through all paths and check if the module name starts with the path name
      for p in pathArray
        if root.molo.paths[p] and name.indexOf("#{root.molo.paths[p]}#{pathSep}") is 0
          prePath = root.molo.paths[p]

      scriptPath = if prePath then "#{prePath}#{pathSep}#{name}.js" else "#{name}.js"

  # Cache and queue definition
  cache = {}
