Fix dependency loading

Loading an IM with dependencies resulted in infinite recursion.
This patch fixes it by loading the dependency only if it's
not loaded already.
Better dependency loading

Improved by Niklas.
Use $.when with chaining