Index: src/bondgraph.cpp
===================================================================
--- src/bondgraph.cpp	(revision 1cbf477d77154171f9c093ad6d333a48cd3d068b)
+++ src/bondgraph.cpp	(revision f18185ccc3610b5c8c0eae54138d97f1e55c8bca)
@@ -209,8 +209,8 @@
               // on other atom(Runner) we check for bond to interface element
               for (BondList::const_iterator BondRunner = Runner->ListOfBonds.begin(); BondRunner != Runner->ListOfBonds.end(); BondRunner++) {
-                atom * const OtherAtom = (*BondRunner)->GetOtherAtom(Walker);
+                atom * const OtherAtom = (*BondRunner)->GetOtherAtom(Runner);
                 if (!OtherHydrogenFlag && (OtherAtom->type->Z == 1)) {
                   OtherHydrogen = OtherAtom;
-                  OtherHydrogen = true;
+                  OtherHydrogenFlag = true;
                 }
                 InterfaceFlag = InterfaceFlag || (OtherAtom->type == InterfaceElement);
@@ -227,6 +227,6 @@
                 if ((Walker->nr < Runner->nr) || (!OtherHydrogenFlag)) {
                   // check angle
-                  OHBond.CopyVector(&Walker->x);
-                  OHBond.SubtractVector(&Hydrogen->x);
+                  OHBond.CopyVector(&Hydrogen->x);
+                  OHBond.SubtractVector(&Walker->x);
                   OOBond.CopyVector(&Runner->x);
                   OOBond.SubtractVector(&Walker->x);
@@ -246,2 +246,75 @@
   return count;
 }
+
+/** Counts the number of bonds between two given elements.
+ * \param *molecules list of molecules with all atoms
+ * \param *first pointer to first element
+ * \param *second pointer to second element
+ * \return number of found bonds (\a *first-\a *second)
+ */
+int CountBondsOfTwo(MoleculeListClass * const molecules, const element * const first, const element * const second)
+{
+  atom *Walker = NULL;
+  int count = 0;
+
+  for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin();MolWalker != molecules->ListOfMolecules.end(); MolWalker++) {
+    Walker = (*MolWalker)->start;
+    while (Walker->next != (*MolWalker)->end) {
+      Walker = Walker->next;
+      if ((Walker->type == first) || (Walker->type == second)) {  // first element matches
+        for (BondList::const_iterator BondRunner = Walker->ListOfBonds.begin(); BondRunner != Walker->ListOfBonds.end(); BondRunner++) {
+          atom * const OtherAtom = (*BondRunner)->GetOtherAtom(Walker);
+          if (((OtherAtom->type == first) || (OtherAtom->type == second)) && (Walker->nr < OtherAtom->nr)) {
+            count++;
+            DoLog(1) && (Log() << Verbose(1) << first->name << "-" << second->name << " bond found between " << *Walker << " and " << *OtherAtom << "." << endl);
+          }
+        }
+      }
+    }
+  }
+  return count;
+};
+
+/** Counts the number of bonds between three given elements.
+ * Note that we do not look for arbitrary sequence of given bonds, but \a *second will be the central atom and we check
+ * whether it has bonds to both \a *first and \a *third.
+ * \param *molecules list of molecules with all atoms
+ * \param *first pointer to first element
+ * \param *second pointer to second element
+ * \param *third pointer to third element
+ * \return number of found bonds (\a *first-\a *second-\a *third, \a *third-\a *second-\a *first, respectively)
+ */
+int CountBondsOfThree(MoleculeListClass * const molecules, const element * const first, const element * const second, const element * const third)
+{
+  int count = 0;
+  bool MatchFlag[2];
+  bool result = false;
+  atom *Walker = NULL;
+  const element * ElementArray[2];
+  ElementArray[0] = first;
+  ElementArray[1] = third;
+
+  for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin();MolWalker != molecules->ListOfMolecules.end(); MolWalker++) {
+    Walker = (*MolWalker)->start;
+    while (Walker->next != (*MolWalker)->end) {
+      Walker = Walker->next;
+      if (Walker->type == second) {  // first element matches
+        for (int i=0;i<2;i++)
+          MatchFlag[i] = false;
+        for (BondList::const_iterator BondRunner = Walker->ListOfBonds.begin(); BondRunner != Walker->ListOfBonds.end(); BondRunner++) {
+          atom * const OtherAtom = (*BondRunner)->GetOtherAtom(Walker);
+          for (int i=0;i<2;i++)
+            MatchFlag[i] = MatchFlag[i] || (OtherAtom->type == ElementArray[i]);
+        }
+        result = true;
+        for (int i=0;i<2;i++)
+          result = result && MatchFlag[i];
+        if (result) {
+          count++;
+          DoLog(1) && (Log() << Verbose(1) << first->name << "-" << second->name << "-" << third->name << " bond found at " << *Walker << "." << endl);
+        }
+      }
+    }
+  }
+  return count;
+};
Index: src/bondgraph.hpp
===================================================================
--- src/bondgraph.hpp	(revision 1cbf477d77154171f9c093ad6d333a48cd3d068b)
+++ src/bondgraph.hpp	(revision f18185ccc3610b5c8c0eae54138d97f1e55c8bca)
@@ -56,4 +56,6 @@
 
 int CountHydrogenBridgeBonds(MoleculeListClass * const molecules, element * InterfaceElement);
+int CountBondsOfTwo(MoleculeListClass * const molecules, const element * const first, const element * const second);
+int CountBondsOfThree(MoleculeListClass * const molecules, const element * const first, const element * const second, const element * const third);
 
 #endif /* BONDGRAPH_HPP_ */
Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision 1cbf477d77154171f9c093ad6d333a48cd3d068b)
+++ src/builder.cpp	(revision f18185ccc3610b5c8c0eae54138d97f1e55c8bca)
@@ -1089,6 +1089,9 @@
   Log() << Verbose(0) << "===========MERGE MOLECULES=====================" << endl;
   Log() << Verbose(0) << "a - simple add of one molecule to another" << endl;
+  Log() << Verbose(0) << "b - count the number of bonds of two elements" << endl;
+  Log() << Verbose(0) << "B - count the number of bonds of three elements " << endl;
   Log() << Verbose(0) << "e - embedding merge of two molecules" << endl;
   Log() << Verbose(0) << "h - count the number of hydrogen bonds" << endl;
+  Log() << Verbose(0) << "b - count the number of hydrogen bonds" << endl;
   Log() << Verbose(0) << "m - multi-merge of all molecules" << endl;
   Log() << Verbose(0) << "s - scatter merge of two molecules" << endl;
@@ -1124,4 +1127,56 @@
       }
       break;
+
+    case 'b':
+      {
+        const int nr = 2;
+        char *names[nr] = {"first", "second"};
+        int Z[nr];
+        element *elements[nr];
+        for (int i=0;i<nr;i++) {
+          Z[i] = 0;
+          do {
+            cout << "Enter " << names[i] << " element: ";
+            cin >> Z[i];
+          } while ((Z[i] <= 0) && (Z[i] > MAX_ELEMENTS));
+          elements[i] = periode->FindElement(Z[i]);
+        }
+        const int count = CountBondsOfTwo(molecules, elements[0], elements[1]);
+        cout << endl << "There are " << count << " ";
+        for (int i=0;i<nr;i++) {
+          if (i==0)
+            cout << elements[i]->symbol;
+          else
+            cout << "-" << elements[i]->symbol;
+        }
+        cout << " bonds." << endl;
+      }
+    break;
+
+    case 'B':
+      {
+        const int nr = 3;
+        char *names[nr] = {"first", "second", "third"};
+        int Z[nr];
+        element *elements[nr];
+        for (int i=0;i<nr;i++) {
+          Z[i] = 0;
+          do {
+            cout << "Enter " << names[i] << " element: ";
+            cin >> Z[i];
+          } while ((Z[i] <= 0) && (Z[i] > MAX_ELEMENTS));
+          elements[i] = periode->FindElement(Z[i]);
+        }
+        const int count = CountBondsOfThree(molecules, elements[0], elements[1], elements[2]);
+        cout << endl << "There are " << count << " ";
+        for (int i=0;i<nr;i++) {
+          if (i==0)
+            cout << elements[i]->symbol;
+          else
+            cout << "-" << elements[i]->symbol;
+        }
+        cout << " bonds." << endl;
+      }
+    break;
 
     case 'e':
