diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh
index f121cdc40ae3b7bbd48cc7ec58e90e095558e43c..eca3e66396085bfef587c7bca936e44898774dda 100644
--- a/src/Lib/Utils.hh
+++ b/src/Lib/Utils.hh
@@ -51,6 +51,9 @@ concept PropertyConstViewable = requires(T element)
     { element.getProperties() }  -> std::same_as<QJsonDocument>;
     // clang-format on
 };
+
+template <class T, class U>
+concept Derived = std::is_base_of<U, T>::value;
 }
 
 namespace Vivy::Utils
diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc
index 01ebb7bcea828edeb701ef4a269394d0d66a4e1c..5615f900219518d07187144a59617c6a7644f960 100644
--- a/src/UI/MainWindow.cc
+++ b/src/UI/MainWindow.cc
@@ -164,19 +164,19 @@ MainWindow::MainWindow() noexcept
     newDocument();
 }
 
+MainWindow::~MainWindow() noexcept { qDebug() << "Closing the main window!"; }
+
 void
 MainWindow::updateFakeVimUsage(bool yes) noexcept
 {
-    const int count = documents->count();
-    for (int index = 0; index < count; ++index) {
-        AbstractDocumentView *docView = getTab(index);
-        const bool documentExists     = docView && docView->getDocument();
+    forEachViews<AbstractDocumentView>([yes](AbstractDocumentView *docView, int) noexcept -> void {
+        const bool documentExists = docView && docView->getDocument();
         const bool isScriptEditor =
             documentExists && (docView->getDocument()->getType() == AbstractDocument::Type::Script);
 
         if (isScriptEditor)
             static_cast<ScriptDocumentView *>(docView)->setUseFakeVimEditor(yes);
-    }
+    });
 }
 
 void
@@ -277,13 +277,12 @@ MainWindow::closeDocument(AbstractDocumentView *const view) noexcept
     if (view == nullptr || !view->getDocument())
         return;
 
-    const int count = documents->count();
-    for (int index = 0; index < count; ++index) {
-        const AbstractDocumentView *const documentView = getTab(index);
-        const bool documentExists = documentView && documentView->getDocument();
-        if (documentExists && (*documentView->getDocument()) == (*view->getDocument()))
-            closeDocument(index);
-    }
+    forEachViews<AbstractDocumentView>(
+        [this, view](AbstractDocumentView *documentView, int index) noexcept -> void {
+            const bool documentExists = documentView && documentView->getDocument();
+            if (documentExists && (*documentView->getDocument()) == (*view->getDocument()))
+                closeDocument(index);
+        });
 }
 
 void
@@ -431,21 +430,21 @@ MainWindow::getIndexOfTab(const AbstractDocumentView *const viewA) const noexcep
     if (docA == nullptr)
         return -1;
 
-    const int count = documents->count();
-    for (int index = 0; index < count; ++index) {
-        const AbstractDocumentView *const viewB = getTab(index);
-        if (viewB == nullptr)
-            continue;
+    int returnIndex = -1;
+    forEachViews<AbstractDocumentView>(
+        [docA, &returnIndex](AbstractDocumentView *viewB, int index) noexcept -> void {
+            if (viewB == nullptr)
+                return;
 
-        const AbstractDocument *const docB = viewB->getDocument();
-        if (docB == nullptr)
-            continue;
+            const AbstractDocument *const docB = viewB->getDocument();
+            if (docB == nullptr)
+                return;
 
-        if (*docB == *docA)
-            return index;
-    }
+            if (*docB == *docA)
+                returnIndex = index;
+        });
 
-    return -1;
+    return returnIndex;
 }
 
 AbstractDocumentView *
@@ -472,17 +471,14 @@ MainWindow::getTab(const int index) const noexcept
 int
 MainWindow::findFirstUntouchedDocument() const noexcept
 {
-    const int count = documents->count();
-    for (int index = 0; index < count; ++index) {
-        AbstractDocumentView *documentView = getTab(index);
-        if (documentView && (documentView->getType() == AbstractDocumentView::Type::Vivy) &&
-            static_cast<VivyDocumentView *>(documentView)
-                ->getDocument()
-                ->checkDocumentOption(VivyDocument::UntouchedByDefault))
-            return index;
-    }
+    int returnIndex = -1;
+    forEachViews<VivyDocumentView>(
+        [&returnIndex](VivyDocumentView *view, int index) noexcept -> void {
+            if (view->getDocument()->checkDocumentOption(VivyDocument::UntouchedByDefault))
+                returnIndex = index;
+        });
 
-    return -1;
+    return returnIndex;
 }
 
 void
diff --git a/src/UI/MainWindow.hh b/src/UI/MainWindow.hh
index 16b379e87bcb15288b8ab739fae0f70607560e5e..d268f160418675e7bb4543ba4b376dfcc8103f73 100644
--- a/src/UI/MainWindow.hh
+++ b/src/UI/MainWindow.hh
@@ -25,6 +25,7 @@ class MainWindow final : public QMainWindow {
 
 public:
     explicit MainWindow() noexcept;
+    ~MainWindow() noexcept override;
 
     AbstractDocument *getCurrentDocument() const;
     template <class Document> Document *getCurrentDocument() const
@@ -77,6 +78,19 @@ private:
         }
     }
 
+    // Walk throu all views in the QTabWidget and also get the index of the tab
+    template <Derived<AbstractDocumentView> DocViewType> void forEachViews(auto callback) const
+    {
+        const int count = documents->count();
+        for (int index = 0; index < count; ++index) {
+            AbstractDocumentView *docView = getTab(index);
+            if constexpr (std::is_same_v<AbstractDocumentView, DocViewType>)
+                callback(docView, index);
+            else if (DocViewType *view = dynamic_cast<DocViewType *>(docView); view != nullptr)
+                callback(view, index);
+        }
+    }
+
 private slots:
     void newDocument() noexcept;
     void openDocument() noexcept;