diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py
index 320ab0897a31d034a7c3337fcbbb8170617f21f8..2b1e99b10cd5629558b1fa17402449c492b5a887 100644
--- a/cal_tools/cal_tools/tools.py
+++ b/cal_tools/cal_tools/tools.py
@@ -73,25 +73,31 @@ def prepare_plots(run_path, threshold=1000000):
     """
     print('Convert svg to pdf and png')
     run_path = os.path.abspath(run_path)
-    files = glob.glob('{}/*_files/*svg'.format(run_path))
 
-    for f_path in files:
-        dir_name = os.path.dirname(f_path)
-        f_name = os.path.basename(f_path)
+    rst_files = glob.glob('{}/*rst'.format(run_path))
+    for rst_file in rst_files:
+        rst_file_name = os.path.basename(rst_file)
+        rst_file_name = os.path.splitext(rst_file_name)[0]
 
-        if (os.stat(f_path)).st_size < threshold:
-            check_call(["svg2pdf", "{}".format(f_path)], shell=False)
-            new_f_name = os.path.splitext(f_name)[0] + '.pdf'
-        else:
-            check_call(["convert", "{}".format(f_path),
-                        "{}.png".format(f_path[:-4])], shell=False)
-            new_f_name = os.path.splitext(f_name)[0] + '.png'
-
-        check_call(["sed",
-                    "-i",
-                    "s/{}/{}/g".format(f_name, new_f_name),
-                    "{}.rst".format(dir_name[:-6])],
-                   shell=False)
+        svg_files = glob.glob(
+            '{}/{}_files/*svg'.format(run_path, rst_file_name))
+        for f_path in svg_files:
+            f_name = os.path.basename(f_path)
+            f_name = os.path.splitext(f_name)[0]
+
+            if (os.stat(f_path)).st_size < threshold:
+                check_call(["svg2pdf", "{}".format(f_path)], shell=False)
+                new_ext = 'pdf'
+            else:
+                check_call(["convert", "{}".format(f_path),
+                            "{}.png".format(f_name)], shell=False)
+                new_ext = 'png'
+
+            check_call(["sed",
+                        "-i",
+                        "s/{}.svg/{}.{}/g".format(f_name, f_name, new_ext),
+                        rst_file],
+                       shell=False)
 
 
 def make_report(run_path, tmp_path, out_path, project, author, version, report_to):