anchor.diff:          |> Seq.map (fun p -> p.GroupName, getPackageOrderResolvedPackage p.Packages)
           |> Map.ofSeq
   
  +    let getDllOrder (dllFiles : AssemblyDefinition list) =
  +      // we ignore all unknown references as they are most likely resolved on package level
  +      let known = dllFiles |> Seq.map (fun a -> a.FullName) |> Set.ofSeq
  +      getPackageOrderGeneric
  +        (fun (p:AssemblyDefinition) -> p.FullName)
  +        (fun p -> p.MainModule.AssemblyReferences |> Seq.map (fun r -> r.FullName) |> Seq.filter (known.Contains))
  +        dllFiles
   
   module ScriptGeneratingModule =
  -  open System.IO
  -  open System.Collections.Generic
  -  open Mono.Cecil
  -  open QuickGraph
  -  open System
  -
  -  let private listOfFrameworks = [
  -    Paket.FrameworkIdentifier.DotNetFramework Paket.FrameworkVersion.V4_5
  -    Paket.FrameworkIdentifier.DotNetFramework Paket.FrameworkVersion.V4_Client
  -    Paket.FrameworkIdentifier.DotNetFramework Paket.FrameworkVersion.V3_5
  -    Paket.FrameworkIdentifier.DotNetFramework Paket.FrameworkVersion.V2
  -  ]
  -
  -  let tryFind key (dict: IDictionary<_,_>) =
  -    match dict.TryGetValue(key) with
  -    | true, v -> Some v
  -    | _       -> None
  -  let weightTargetProfiles possibleFrameworksInOrderOfPreference (profiles: Paket.TargetProfile list) =
  -  
  -    let relevantFrameworksWithPreferenceIndex =
  -      possibleFrameworksInOrderOfPreference
  -      |> Seq.mapi (fun i f -> f, i)
  -      |> dict
  -  
  -    let profileWithAllFrameworks =
  -      profiles
  -      |> Seq.map (fun targetProfile ->
  -        targetProfile, 
  -        match targetProfile with
  -        | Paket.SinglePlatform platform -> [platform]
  -        | Paket.PortableProfile (_, platforms) -> platforms
  -      )
  -      |> Seq.map (fun (targetProfile, profiles) ->
  -        targetProfile, 
  -        profiles
  -        |> Seq.map (fun p -> p, tryFind p relevantFrameworksWithPreferenceIndex)
  -      )
  -  
  -    // we pick the first one for which the sum of prefered indexes is the lowest
  -    let choices = 
  -      profileWithAllFrameworks
  -      |> Seq.map (fun (targetProfile, selection) -> 
  -        let sum =
  -          let selectionWithMatches =
  -            selection
  -            |> Seq.filter (snd >> Option.isSome)
  -          if selectionWithMatches |> Seq.isEmpty then
  -            Int32.MaxValue
  -          else
  -            selectionWithMatches
  -            |> Seq.map (snd)
  -            |> Seq.choose id
  -            |> Seq.sum
  -        targetProfile,sum
  -      )
  -      |> Seq.filter (snd >> ((<>) Int32.MaxValue))
  -    choices
  -
  -  let makeTargetPredicate frameworks =
  -    fun (folders: Paket.LibFolder list) ->
  -      folders
  -      |> Seq.map (fun folder-> folder, weightTargetProfiles frameworks folder.Targets)
  -      |> Seq.map (fun (folder, weightedTargetProfiles) -> 
  -        folder, weightedTargetProfiles |> Seq.sumBy snd
  -      )
  -      |> Seq.sortBy snd
  -      |> Seq.map fst
  -      |> Seq.tryHead
  -
  -  let getDllFilesWithinPackage (paketDependencies: Paket.Dependencies) (targetPredicate: _ -> Paket.LibFolder option) groupName packageName =
  -  
  -    let installModel = paketDependencies.GetInstalledPackageModel(groupName, packageName)
  -    let libFolder = targetPredicate installModel.ReferenceFileFolders
  -  
  -    let references = 
  -      match libFolder with
  -      | None        -> Set.empty
  -      | Some folder -> folder.Files.References
  -
  -    let referenceByAssembly =
  -      references
  -      |> Seq.filter (fun f -> f.LibName.IsSome)
  -      |> Seq.map (fun r -> (r.Path |> AssemblyDefinition.ReadAssembly), r)
  +  let getDllFilesWithinPackage (framework: FrameworkIdentifier) (installModel :InstallModel) =
  +    let dllFiles =
  +      installModel
  +      |> InstallModel.getLibReferences (SinglePlatform framework)
  +      |> Seq.map (fun path -> AssemblyDefinition.ReadAssembly path, path)
         |> dict
   
  -    let assemblyByName =
  -      referenceByAssembly.Keys
  -      |> Seq.map (fun a -> a.Name.ToString(), a)
  -      |> dict
  +    LoadingScriptsGenerator.getDllOrder (dllFiles.Keys |> Seq.toList)
  +      |> List.map (fun a -> dllFiles.[a])
   
  -    let graph = AdjacencyGraph<_,_>()
  -  
  -    for r in referenceByAssembly do
  -      let assembly = r.Key
  -      let paketRef = referenceByAssembly.[assembly]
  -      graph.AddVertex(paketRef) |> ignore
  -      let references = assembly.MainModule.AssemblyReferences
  -      printfn "%A" paketRef
  -      references
  -      |> Seq.map (fun a -> tryFind a.FullName assemblyByName)
  -      |> Seq.choose id
  -      |> Seq.map (fun a -> paketRef, referenceByAssembly.[a])
  -      |> Seq.iter (fun (fromRef, toRef) ->
  -        graph.AddVertex(toRef) |> ignore
  -        graph.AddEdge(Edge(fromRef, toRef)) |> ignore
  -        )
  +  type ScriptGenInput =
  +    { Framework : FrameworkIdentifier
  +      PackagesOrGroupFolder : DirectoryInfo
  +      PackageInstallModel : InstallModel
  +      DependentScripts : string seq }
   
  -    let result =
  -      let topologicalSort = Algorithms.TopologicalSort.TopologicalSortAlgorithm(graph)
  -      topologicalSort.Compute()
  -      topologicalSort.SortedVertices |> Seq.rev |> Seq.toArray
  -
  -    result
  -
  -  let getScriptName (package: PackageName) = sprintf "Include_%s.fsx" (package.GetCompareString())
  -  let generateFSharpScript dependenciesFile lockFile (packagesOrGroupFolder: DirectoryInfo) (knownIncludeScripts:Map<PackageName, string>) (package: PackageResolver.ResolvedPackage) =
  -    let packageFolder = 
  -      Path.Combine (packagesOrGroupFolder.FullName, package.Name.GetCompareString())
  -      |> DirectoryInfo
  -
  -    let scriptFile = Path.Combine (packageFolder.FullName, getScriptName package.Name)
  -    let relScriptFile = Path.Combine (package.Name.GetCompareString(), getScriptName package.Name)
  -    let depLines =
  -      package.Dependencies
  -      |> Seq.map (fun (depName,_,_) -> sprintf "#load \"../%s\"" ((knownIncludeScripts |> Map.find depName).Replace("\\", "/")))
  -    
  -    let toRelative = (Path.GetFullPath >> (fun f -> f.Substring(packageFolder.FullName.Length + 1)))
  -
  -    let dllFiles =
  -      if package.Name.GetCompareString().ToLowerInvariant() = "fsharp.core" then
  -        Seq.empty
  -      else
  -        let group = None
  -        let references = getDllFilesWithinPackage dependenciesFile (makeTargetPredicate listOfFrameworks) group (package.Name.GetCompareString())
  -        references
  -        |> Seq.map (fun r -> toRelative r.Path)
  -
  -    let orderedDllFiles =
  -      // TODO: Order by the inter-dependencies
  -      // 1. Drop all unknown dependencies (they are either already resolved or we cannot do it anyway)
  -      // 2. Use the algorithm above to sort.
  -      dllFiles
  -      |> Seq.sortBy (fun l -> l.Length)
  -      
  -    let dllLines =
  -      orderedDllFiles
  -      |> Seq.map (fun dll -> sprintf "#r \"%s\"" (dll.Replace("\\", "/")))
  -
  -    depLines
  -    |> fun lines -> Seq.append lines dllLines
  -    |> fun lines -> Seq.append lines [ sprintf "printfn \"%%s\" \"Loaded %s\"" (package.Name.GetCompareString()) ]
  -    |> fun lines -> File.WriteAllLines (scriptFile, lines)
  -    
  -    knownIncludeScripts |> Map.add package.Name relScriptFile
  -    
     // Generate a fsharp script from the given order of packages, if a package is ordered before its dependencies this function will throw.
  -  let generateFSharpScripts dependenciesFile lockFile packagesOrGroupFolder (orderedPackages: PackageResolver.ResolvedPackage list) =
  +  let generateScripts
  +    getScriptName
  +    (scriptGenerator: ScriptGenInput -> string seq)
  +    (framework: FrameworkIdentifier)
  +    (dependenciesFile:Dependencies)
  +    (packagesOrGroupFolder:DirectoryInfo)
  +    groupName
  +    (orderedPackages: PackageResolver.ResolvedPackage list) =
         orderedPackages
  -      |> Seq.fold (fun (knownIncludeScripts) p ->
  -        generateFSharpScript dependenciesFile lockFile packagesOrGroupFolder knownIncludeScripts p) Map.empty
  +      |> Seq.fold (fun (knownIncludeScripts:Map<PackageName,string>) (p:PackageResolver.ResolvedPackage) ->
  +        let scriptFile = Path.Combine (packagesOrGroupFolder.FullName, p.Name.GetCompareString(), getScriptName framework p.Name)
  +        let relScriptFile = Path.Combine (p.Name.GetCompareString(), getScriptName framework p.Name)
  +        let dependencies = p.Dependencies |> Seq.map (fun (depName,_,_) -> knownIncludeScripts.[depName])
  +        let installModel = dependenciesFile.GetInstalledPackageModel(groupName, p.Name.GetCompareString())
  +        let lines =
  +          scriptGenerator
  +            { Framework = framework
  +              PackagesOrGroupFolder = packagesOrGroupFolder
  +              PackageInstallModel = installModel
  +              DependentScripts = dependencies }
  +        File.WriteAllLines (scriptFile, lines)
  +        knownIncludeScripts |> Map.add p.Name relScriptFile) Map.empty
         |> ignore
   
  -        
     // Generate a fsharp script from the given order of packages, if a package is ordered before its dependencies this function will throw.
  -  let generateFSharpScriptsForRootFolder (rootFolder: DirectoryInfo) =
  +  let generateScriptsForRootFolder getScriptName scriptGenerator (framework: FrameworkIdentifier) (rootFolder: DirectoryInfo)  =
         
         let dependenciesFile, lockFile =
             let deps = Paket.Dependencies.Locate(rootFolder.FullName)
  
positive.code: 
        dependencies
        |> Map.map (fun groupName packages ->
          let packagesOrGroupFolder =
            match groupName.GetCompareString () with
            | "main"    -> packagesFolder 
            | groupName -> Path.Combine(packagesFolder.FullName, groupName) |> DirectoryInfo
          generateFSharpScripts dependenciesFile lockFile packagesOrGroupFolder packages
          )
        |> ignore
        
negative.code: open Paket
  open Paket.Domain
  open System.IO
  open Mono.Cecil
  
  module LoadingScriptsGenerator =
      let getLeafPackagesGeneric getPackageName getDependencies (knownPackages:Set<_>) (openList) =
          let leafPackages =
            openList 
  