diff --git a/src/nextflow_file.py b/src/nextflow_file.py
index ebdf62d06a2b5796521625fce3f96252d3b6b0bf..9329ac8adc4b33ff1ffcdab055c2426162291c47 100644
--- a/src/nextflow_file.py
+++ b/src/nextflow_file.py
@@ -273,6 +273,26 @@ class Nextflow_File(Nextflow_Building_Blocks):
             modules+=list(include.defines.values())
         return modules
 
+    def get_calls_made_outside_of_main(self):
+        #Code without processes
+        code = self.get_code()
+        for proecess in self.processes:
+            code = code.replace(proecess.get_code(), "")
+        for sub in self.subworkflows:
+            code = code.replace(sub.get_code(), "")
+        for fun in self.functions:
+            code = code.replace(fun.get_code(), "")
+        if(self.first_file and self.main!=None):
+            code = code.replace(self.main.get_code(), "")
+        for include in self.includes:
+            code = code.replace(include.get_code(), "")
+
+        from .root import Root
+        self.root = Root(code=code, origin= self, modules_defined=self.get_modules_defined(), subworkflow_inputs = [])
+        self.root.initialise()
+        calls = {}
+        self.root.get_all_calls_in_subworkflow(calls=calls)
+        return list(calls.keys())
 
     #----------------------
     #INITIALISE
diff --git a/src/workflow.py b/src/workflow.py
index 277b1c7b0f12a4f34d3a394c86597265d6af2d88..1826ab782973f1da45997b4988d4844e7b11fee4 100644
--- a/src/workflow.py
+++ b/src/workflow.py
@@ -461,21 +461,18 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen
 
         ankers = function_section+ "\n"*3 + process_section+ "\n"*3 + subworkflow_section
 
-        to_replace = []
-        for match in re.finditer(constant.FULL_INLCUDE_2, code):
-            to_replace.append(match.group(0))
+        
 
-        if(to_replace==[]):
-            pos_start = 0
-            start_code_pattern = r"\#\!\s*\/usr\/bin\/env\s+nextflow"
-            for match in re.finditer(start_code_pattern, code):
-                pos_start = match.span(0)[1]+1
-            code = code[:pos_start]+ankers+code[pos_start:]
-        else:
-            for r in to_replace:
-                code = code.replace(r, ankers)
-                ankers = ""
+        #Place ankers
+        pos_start = 0
+        start_code_pattern = r"\#\!\s*\/usr\/bin\/env\s+nextflow"
+        for match in re.finditer(start_code_pattern, code):
+            pos_start = match.span(0)[1]+1
+        code = code[:pos_start]+ankers+code[pos_start:]
         
+        #Remove the includes
+        for match in re.finditer(constant.FULL_INLCUDE_2, code):
+            code = re.sub(fr"{re.escape(match.group(0))}.*", "", code)
 
         processes, subworkflows, functions = [], [], []
         for c in self.get_workflow_main().get_all_calls_in_workflow():
@@ -488,6 +485,14 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen
                 functions.append(ele)
             else:
                 raise Exception("This shoudn't happen")
+        
+        #Get calls to functions made outside of themain which might have been imported -> so we need to add them
+        for c in self.get_first_file().get_calls_made_outside_of_main():
+            ele = c.get_first_element_called()
+            if(ele.get_type()=="Function"):
+                functions.append(ele)
+            else:
+                raise Exception("This shoudn't happen -> either a call to a process or subworkflow outside of main or subworkflow")
 
         #Simplifying main
         code = code.replace(self.get_workflow_main().get_code(get_OG = True), self.get_workflow_main().simplify_code())