diff --git a/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp b/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp
index 20db346963ba19f0260936ba1b3eac9672738408..ee5faecda20afd8df9e339679bfd944a124520fe 100644
--- a/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp
+++ b/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp
@@ -14,10 +14,12 @@
 #include "TTree.h"
 #include "TBranch.h"
 
+#include <iostream>
+
 using namespace pool;
 using namespace std;
 
-RootTreeIndexContainer::RootTreeIndexContainer() : RootTreeContainer(), m_index_ref(nullptr), m_index_foreign(nullptr), m_index_multi(0), m_index(nullptr) {
+RootTreeIndexContainer::RootTreeIndexContainer() : RootTreeContainer(), m_index_ref(nullptr), m_index_entries(0), m_index_multi(0), m_index(nullptr) {
    m_index = new long long int;
    m_index_multi = getpid();
 }
@@ -31,17 +33,10 @@ RootTreeIndexContainer::~RootTreeIndexContainer() {
 long long int RootTreeIndexContainer::nextRecordId()    {
    long long int s = m_index_multi;
    s = s << 32;
-   if (m_tree != nullptr) {
-      if (m_index_foreign != nullptr) {
-         s += m_index_foreign->GetEntries();
-      } else {
-         m_index_foreign = (TBranch*)m_tree->GetBranch("index_ref");
-         if (m_index_foreign != nullptr) {
-            s += m_index_foreign->GetEntries();
-         } else {
-            s += RootTreeContainer::nextRecordId();
-         }
-      }
+   if (m_tree->GetBranch("index_ref") != nullptr) {
+      s += m_index_entries;
+   } else {
+      s += RootTreeContainer::nextRecordId();
    }
    return s;
 }
@@ -49,27 +44,30 @@ long long int RootTreeIndexContainer::nextRecordId()    {
 
 DbStatus RootTreeIndexContainer::writeObject(ActionList::value_type& action) {
    long long int s = 0;
-   if( isBranchContainer() ) {
+   if (isBranchContainer()) {
       TBranch * pBranch = m_tree->GetBranch(m_branchName.c_str());
       if (pBranch != nullptr) s = pBranch->GetEntries();
    } else {
       s = m_tree->GetEntries();
    }
-   if (m_index_ref  == nullptr && m_index_foreign == nullptr) {
-      if (m_tree->GetBranch("index_ref") == nullptr) {
-         m_index_ref = (TBranch*)m_tree->Branch("index_ref", m_index);
-      } else {
-         m_index_foreign = (TBranch*)m_tree->GetBranch("index_ref");
-      }
+   if (m_index_ref == nullptr && m_tree->GetBranch("index_ref") == nullptr) {
+      m_index_ref = (TBranch*)m_tree->Branch("index_ref", m_index);
    }
    if (m_index_ref != nullptr && s >= m_index_ref->GetEntries()) {
       *m_index = this->nextRecordId();
       m_index_ref->SetAddress(m_index);
-      if( isBranchContainer() && !m_treeFillMode ) m_index_ref->Fill();
+      if (isBranchContainer() && !m_treeFillMode) m_index_ref->Fill();
+   }
+   if (isBranchContainer() && !m_treeFillMode) {
+      m_tree->SetEntries(s);
+      m_index_entries++;
    }
-   if( isBranchContainer() && !m_treeFillMode ) m_tree->SetEntries(s);
    DbStatus status = RootTreeContainer::writeObject(action);
-   if( isBranchContainer() && !m_treeFillMode ) m_tree->SetEntries(s + 1);
+   if (isBranchContainer() && !m_treeFillMode) {
+      m_tree->SetEntries(s + 1);
+   } else {
+      m_index_entries++;
+   }
    return status;
 }
 
diff --git a/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.h b/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.h
index 736e52f3f8ad7fe8fa71e8764be155ca3a1afba5..158839e8223aa6aaf92f24fc7a132c67ee41afe0 100644
--- a/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.h
+++ b/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.h
@@ -61,7 +61,7 @@ namespace pool {
    private:
       /// Pointer to index branch
       TBranch* m_index_ref;
-      TBranch* m_index_foreign;
+      long long int m_index_entries;
       /// Index multiplier (e.g. pid - ppid), fill in c'tor
       int m_index_multi;
       /// Index (64 bit)