diff --git a/maxwell_integrate_to_h5.py b/maxwell_integrate_to_h5.py
index 3332b643a87f8b77fe42e3cb178fd0175134b93f..f7d7e509532b9102a3682a4fe89af425ba9518b9 100644
--- a/maxwell_integrate_to_h5.py
+++ b/maxwell_integrate_to_h5.py
@@ -125,36 +125,44 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
             subdir_name = os.path.basename(os.path.normpath(subdir_path_int))
             results_df.to_csv(os.path.join(subdir_path_int, f"{subdir_name}.csv"), index=False)
 
-            # Create the HDF5 file
-            with h5py.File("output_silx_format.h5", "w") as f:
-                # Create top-level group (e.g., subdir_name)
-                subdir_grp = f.create_group(subdir_name)
-                subdir_grp.attrs["NX_class"] = np.string_("NXentry")
-                subdir_grp.attrs["description"] = np.string_(subdir_name)
+            def natural_sort_key(item):
+                return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', item["filename"])]
+
+            results_data = sorted(results_data, key=natural_sort_key)
+            
+            output_file = os.path.join(subdir_path_int, f"{subdir_name}.h5")
+            if os.path.exists(output_file):
+                
+                print(f"File {output_file} already exists. Removing it to create a new one.")
+                os.remove(output_file)
+
+            with h5py.File(output_file, "w") as f:
+                entry = f.create_group("entry")
+                entry.attrs["NX_class"] = np.string_("NXentry")
+                entry.attrs["default"] = np.string_("1.1")  # Optional
 
                 for idx, result in enumerate(results_data, start=1):
-                    scan_name = f"{idx}.1"
-                    scan_grp = subdir_grp.create_group(scan_name)
-
-                    # --- Group-level attributes ---
-                    scan_grp.attrs["NX_class"] = np.string_("NXentry")
-                    scan_grp.attrs["default"] = np.string_("measurement")
-                    scan_grp.attrs["plotselect"] = np.string_("q,I")
-
-                    # --- Measurement group ---
-                    meas_grp = scan_grp.create_group("measurement")
-                    meas_grp.attrs["NX_class"] = np.string_("NXcollection")
-
-                    # Datasets: q, I, dI
-                    for name in ["q", "I", "dI"]:
-                        data = result[name]
-                        meas_grp.create_dataset(name, data=data, chunks=(min(len(data), 1000),), dtype='f8')
-
-                    # --- Plotselect group ---
-                    plotselect_grp = scan_grp.create_group("plotselect")
-                    plotselect_grp.attrs["NX_class"] = np.string_("NXcollection")
-                    plotselect_grp.attrs["axes"] = np.string_("q")
-                    plotselect_grp.attrs["signal"] = np.string_("I")
+                    group_name = f"{idx}.1"
+                    g = entry.create_group(group_name)
+                    g.attrs["NX_class"] = np.string_("NXentry")
+                    g.attrs["default"] = np.string_("measurement")
+
+                    meas = g.create_group("measurement")
+                    meas.attrs["NX_class"] = np.string_("NXdata")
+                    meas.attrs["signal"] = np.string_("I")
+                    meas.attrs["axes"] = np.string_("q")
+                    meas.attrs["filename"] = np.string_(result["filename"])  # 👈 capture filename
+                    meas.attrs["title"] = np.string_(result["filename"])     # (H5Web may show this)
+
+                    # Store datasets
+                    meas.create_dataset("q", data=result["q"])
+                    meas.create_dataset("I", data=result["I"])
+                    meas.create_dataset("dI", data=result["dI"])
+
+                    # Optional: add filename as long_name attribute on I
+                    meas["I"].attrs["long_name"] = np.string_(result["filename"])
+
+            print(f"✅ HDF5 file '{output_file}' created with {len(results_data)} spectra.")
             # # Sort results_data by filename
             # def natural_sort_key(item):
             #     return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', item["filename"])]
@@ -178,8 +186,6 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
             #         },
             #     }
 
-            print(f"Results for subdirectory {subdir_name} saved to HDF5 file using h5py.")
-
             # Save to HDF5 file using silx
             # hdf5_file_path = os.path.join(subdir_path_int, f"{subdir_name}.h5")
             # dicttoh5(hdf5_data, hdf5_file_path, mode="w")