Index: configure.ac
===================================================================
--- configure.ac	(revision 69948e5f49ca5ca15fa7f148e0e7d53f7f814866)
+++ configure.ac	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -115,6 +115,6 @@
 #AC_MSG_NOTICE(["GLU_CFLAGS: $GLU_CFLAGS, GLU_CXXFLAGS: $GLU_CXXFLAGS, GLU_LDFLAGS: $GLU_LDFLAGS, GLU_LIBS: $GLU_LIBS"])
 
-# CodePatterns library (needs default assert action to be abort and LOG() macro)
-AM_PATH_CODEPATTERNS([1.0.8], $have_debug,,[AC_MSG_ERROR([Missing CodePatterns library, please specify PKG_CONFIG_PATH or use --with-codepatterns...])])
+# CodePatterns library (needs bugfixed ObservedContainer)
+AM_PATH_CODEPATTERNS([1.0.9], $have_debug,,[AC_MSG_ERROR([Missing CodePatterns library, please specify PKG_CONFIG_PATH or use --with-codepatterns...])])
 
 # Checks for header files.
Index: src/Descriptors/MoleculeOrderDescriptor.cpp
===================================================================
--- src/Descriptors/MoleculeOrderDescriptor.cpp	(revision 69948e5f49ca5ca15fa7f148e0e7d53f7f814866)
+++ src/Descriptors/MoleculeOrderDescriptor.cpp	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -50,7 +50,9 @@
   int i=0;
   molecule *mol = NULL;
-  if (id > 0) {
+  if (id == 0) {
+    return NULL;
+  } else if (id > 0) {
     World::MoleculeSet::const_iterator res = molecules.begin();
-    for (; res != molecules.end(); ++res) {
+    for (; res != molecules.end(); ++res) { // when iterator is normal, ++ goes forward!
       ++i;
       if (id == i)
@@ -60,5 +62,5 @@
   } else {
     World::MoleculeSet::const_reverse_iterator res = molecules.rbegin();
-    for (; res != molecules.rend(); --res) {
+    for (; res != molecules.rend(); --res) {  // when iterator is reverse, ++ goes backward!
       --i;
       if (id == i)
Index: tests/regression/Makefile.am
===================================================================
--- tests/regression/Makefile.am	(revision 69948e5f49ca5ca15fa7f148e0e7d53f7f814866)
+++ tests/regression/Makefile.am	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -87,4 +87,5 @@
 	$(srcdir)/RandomNumbers/Engine/testsuite-set-random-number-engine.at \
 	$(srcdir)/Selection/testsuite-selection.at \
+	$(srcdir)/Selection/Atoms/testsuite-selection-atoms.at \
 	$(srcdir)/Selection/Atoms/AllAtoms/testsuite-selection-all-atoms.at \
 	$(srcdir)/Selection/Atoms/AllAtomsOfMolecule/testsuite-selection-all-atoms-of-molecule.at \
@@ -94,4 +95,5 @@
 	$(srcdir)/Selection/Atoms/AtomsInsideSphere/testsuite-selection-atoms-inside-sphere.at \
 	$(srcdir)/Selection/Atoms/ClearAtoms/testsuite-selection-clear-atoms.at \
+	$(srcdir)/Selection/Molecules/testsuite-selection-molecules.at \
 	$(srcdir)/Selection/Molecules/AllMolecules/testsuite-selection-all-molecules.at \
 	$(srcdir)/Selection/Molecules/ClearMolecules/testsuite-selection-clear-molecules.at \
@@ -99,4 +101,5 @@
 	$(srcdir)/Selection/Molecules/MoleculeById/testsuite-selection-molecule-by-id.at \
 	$(srcdir)/Selection/Molecules/MoleculeByName/testsuite-selection-molecules-by-name.at \
+	$(srcdir)/Selection/Molecules/MoleculeByOrder/testsuite-selection-molecule-by-order.at \
 	$(srcdir)/Selection/Molecules/MoleculeOfAtom/testsuite-selection-atoms-molecules.at \
 	$(srcdir)/Tesselation/testsuite-tesselation.at \
Index: tests/regression/Selection/Atoms/testsuite-selection-atoms.at
===================================================================
--- tests/regression/Selection/Atoms/testsuite-selection-atoms.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Atoms/testsuite-selection-atoms.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,24 @@
+AT_BANNER([MoleCuilder - Atom selections])
+
+### ATOMS ####
+
+# (un)select all atoms
+m4_include(Selection/Atoms/AllAtoms/testsuite-selection-all-atoms.at)
+
+# (un)select all atoms of molecule
+m4_include(Selection/Atoms/AllAtomsOfMolecule/testsuite-selection-all-atoms-of-molecule.at)
+
+# (un)select atoms by element
+m4_include(Selection/Atoms/AtomByElement/testsuite-selection-atoms-by-element.at)
+
+# (un)select atom by id
+m4_include(Selection/Atoms/AtomById/testsuite-selection-atom-by-id.at)
+
+# (un)select atoms inside cuboid
+m4_include(Selection/Atoms/AtomsInsideCuboid/testsuite-selection-atoms-inside-cuboid.at)
+
+# (un)select atoms inside sphere
+m4_include(Selection/Atoms/AtomsInsideSphere/testsuite-selection-atoms-inside-sphere.at)
+
+# clear atom selection
+m4_include(Selection/Atoms/ClearAtoms/testsuite-selection-clear-atoms.at)
Index: tests/regression/Selection/Molecules/MoleculeByOrder/post/empty.xyz
===================================================================
--- tests/regression/Selection/Molecules/MoleculeByOrder/post/empty.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/MoleculeByOrder/post/empty.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,2 @@
+0
+	Created by molecuilder for select-molecule-by-id test
Index: tests/regression/Selection/Molecules/MoleculeByOrder/post/twowater.xyz
===================================================================
--- tests/regression/Selection/Molecules/MoleculeByOrder/post/twowater.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/MoleculeByOrder/post/twowater.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,8 @@
+6
+	Created by molecuilder for select-molecule-by-order test
+O	0	0	0.504284
+H	0.758602	0	1.00857
+H	0.758602	0	0
+O	0	0	3.36143
+H	0.758602	0	3.86571
+H	0.758602	0	2.85714
Index: tests/regression/Selection/Molecules/MoleculeByOrder/post/water_id0.xyz
===================================================================
--- tests/regression/Selection/Molecules/MoleculeByOrder/post/water_id0.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/MoleculeByOrder/post/water_id0.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,5 @@
+3
+	Created by molecuilder for select-molecule-by-order test
+O	0	0	0.504284
+H	0.758602	0	1.00857
+H	0.758602	0	0
Index: tests/regression/Selection/Molecules/MoleculeByOrder/post/water_id1.xyz
===================================================================
--- tests/regression/Selection/Molecules/MoleculeByOrder/post/water_id1.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/MoleculeByOrder/post/water_id1.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,5 @@
+3
+	Created by molecuilder for select-molecule-by-order test
+O	0	0	3.36143
+H	0.758602	0	3.86571
+H	0.758602	0	2.85714
Index: tests/regression/Selection/Molecules/MoleculeByOrder/pre/twowater.xyz
===================================================================
--- tests/regression/Selection/Molecules/MoleculeByOrder/pre/twowater.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/MoleculeByOrder/pre/twowater.xyz	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,8 @@
+6
+	Created by molecuilder for select-molecule-by-order test
+O	0	0	0.504284
+H	0.758602	0	1.00857
+H	0.758602	0	0
+O	0	0	3.36143
+H	0.758602	0	3.86571
+H	0.758602	0	2.85714
Index: tests/regression/Selection/Molecules/MoleculeByOrder/testsuite-selection-molecule-by-order.at
===================================================================
--- tests/regression/Selection/Molecules/MoleculeByOrder/testsuite-selection-molecule-by-order.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/MoleculeByOrder/testsuite-selection-molecule-by-order.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,86 @@
+### (un)select molecules by order
+
+
+AT_SETUP([Selection - Molecule by order, forward])
+AT_KEYWORDS([selection,molecule])
+# some variables before
+srcpath="Selection/Molecules/MoleculeByOrder"
+srcfile=twowater.xyz
+m4_include(CheckCommand.sh)
+# the tests
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-molecule-by-order 1 -s water_id0.xyz"
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-molecule-by-order 2 -s water_id1.xyz"
+mv water_id0.xyz water_id0_a.xyz
+mv water_id1.xyz water_id1_a.xyz
+check_command_output $srcpath $srcfile "empty.xyz" "-I --select-molecule-by-order 1 --undo -s empty.xyz"
+check_command_output $srcpath $srcfile "empty.xyz" "-I --select-molecule-by-order 2 --undo -s empty.xyz"
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-molecule-by-order 1 --undo --redo -s water_id0.xyz"
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-molecule-by-order 2 --undo --redo -s water_id1.xyz"
+mv water_id0.xyz water_id0_b.xyz
+mv water_id1.xyz water_id1_b.xyz
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id0_a.xyz water_id0_b.xyz], 0, [ignore], [ignore])
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id1_a.xyz water_id1_b.xyz], 0, [ignore], [ignore])
+AT_CLEANUP
+
+AT_SETUP([Selection - Molecule by order, backward])
+AT_KEYWORDS([selection,molecule])
+# some variables before
+srcpath="Selection/Molecules/MoleculeByOrder"
+srcfile=twowater.xyz
+m4_include(CheckCommand.sh)
+# the tests
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-molecule-by-order -2 -s water_id0.xyz"
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-molecule-by-order -1 -s water_id1.xyz"
+mv water_id0.xyz water_id0_a.xyz
+mv water_id1.xyz water_id1_a.xyz
+check_command_output $srcpath $srcfile "empty.xyz" "-I --select-molecule-by-order -2 --undo -s empty.xyz"
+check_command_output $srcpath $srcfile "empty.xyz" "-I --select-molecule-by-order -1 --undo -s empty.xyz"
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-molecule-by-order -2 --undo --redo -s water_id0.xyz"
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-molecule-by-order -1 --undo --redo -s water_id1.xyz"
+mv water_id0.xyz water_id0_b.xyz
+mv water_id1.xyz water_id1_b.xyz
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id0_a.xyz water_id0_b.xyz], 0, [ignore], [ignore])
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id1_a.xyz water_id1_b.xyz], 0, [ignore], [ignore])
+AT_CLEANUP
+
+AT_SETUP([Unselection - Molecule by order, forward])
+AT_KEYWORDS([selection,molecule])
+# some variables before
+srcpath="Selection/Molecules/MoleculeByOrder"
+srcfile=twowater.xyz
+m4_include(CheckCommand.sh)
+# the tests
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-all-molecules --unselect-molecule-by-order 1 -s water_id1.xyz"
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-all-molecules --unselect-molecule-by-order 2 -s water_id0.xyz"
+mv water_id1.xyz water_id1_a.xyz
+mv water_id0.xyz water_id0_a.xyz
+check_command_output $srcpath $srcfile "twowater.xyz" "-I --select-all-molecules --unselect-molecule-by-order 1 --undo -s twowater.xyz"
+check_command_output $srcpath $srcfile "twowater.xyz" "-I --select-all-molecules --unselect-molecule-by-order 2 --undo -s twowater.xyz"
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-all-molecules --unselect-molecule-by-order 1 --undo --redo -s water_id1.xyz"
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-all-molecules --unselect-molecule-by-order 2 --undo --redo -s water_id0.xyz"
+mv water_id0.xyz water_id0_b.xyz
+mv water_id1.xyz water_id1_b.xyz
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id0_a.xyz water_id0_b.xyz], 0, [ignore], [ignore])
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id1_a.xyz water_id1_b.xyz], 0, [ignore], [ignore])
+AT_CLEANUP
+
+AT_SETUP([Unselection - Molecule by order, backward])
+AT_KEYWORDS([selection,molecule])
+# some variables before
+srcpath="Selection/Molecules/MoleculeByOrder"
+srcfile=twowater.xyz
+m4_include(CheckCommand.sh)
+# the tests
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-all-molecules --unselect-molecule-by-order -2 -s water_id1.xyz"
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-all-molecules --unselect-molecule-by-order -1 -s water_id0.xyz"
+mv water_id1.xyz water_id1_a.xyz
+mv water_id0.xyz water_id0_a.xyz
+check_command_output $srcpath $srcfile "twowater.xyz" "-I --select-all-molecules --unselect-molecule-by-order -2 --undo -s twowater.xyz"
+check_command_output $srcpath $srcfile "twowater.xyz" "-I --select-all-molecules --unselect-molecule-by-order -1 --undo -s twowater.xyz"
+check_command_output $srcpath $srcfile "water_id1.xyz" "-I --select-all-molecules --unselect-molecule-by-order -2 --undo --redo -s water_id1.xyz"
+check_command_output $srcpath $srcfile "water_id0.xyz" "-I --select-all-molecules --unselect-molecule-by-order -1 --undo --redo -s water_id0.xyz"
+mv water_id0.xyz water_id0_b.xyz
+mv water_id1.xyz water_id1_b.xyz
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id0_a.xyz water_id0_b.xyz], 0, [ignore], [ignore])
+AT_CHECK([diff -I '.*Created by molecuilder.*' water_id1_a.xyz water_id1_b.xyz], 0, [ignore], [ignore])
+AT_CLEANUP
Index: tests/regression/Selection/Molecules/testsuite-selection-molecules.at
===================================================================
--- tests/regression/Selection/Molecules/testsuite-selection-molecules.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
+++ tests/regression/Selection/Molecules/testsuite-selection-molecules.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -0,0 +1,24 @@
+AT_BANNER([MoleCuilder - Molecule selections])
+
+### Molecules ###
+
+# (un)select all molecules
+m4_include(Selection/Molecules/AllMolecules/testsuite-selection-all-molecules.at)
+
+# clear molecule selection
+m4_include(Selection/Molecules/ClearMolecules/testsuite-selection-clear-molecules.at)
+
+# (un)select molecules by formula
+m4_include(Selection/Molecules/MoleculeByFormula/testsuite-selection-molecules-by-formula.at)
+
+# (un)select molecule by id
+m4_include(Selection/Molecules/MoleculeById/testsuite-selection-molecule-by-id.at)
+
+# (un)select molecule by name
+m4_include(Selection/Molecules/MoleculeByName/testsuite-selection-molecules-by-name.at)
+
+# (un)select molecule by order
+m4_include(Selection/Molecules/MoleculeByOrder/testsuite-selection-molecule-by-order.at)
+
+# (un)select molecule from atom selection
+m4_include(Selection/Molecules/MoleculeOfAtom/testsuite-selection-atoms-molecules.at)
Index: tests/regression/Selection/testsuite-selection.at
===================================================================
--- tests/regression/Selection/testsuite-selection.at	(revision 69948e5f49ca5ca15fa7f148e0e7d53f7f814866)
+++ tests/regression/Selection/testsuite-selection.at	(revision 170ba6e440672fc3912107325618abdd35c6437e)
@@ -3,42 +3,7 @@
 ### ATOMS ####
 
-# (un)select all atoms
-m4_include(Selection/Atoms/AllAtoms/testsuite-selection-all-atoms.at)
-
-# (un)select all atoms of molecule
-m4_include(Selection/Atoms/AllAtomsOfMolecule/testsuite-selection-all-atoms-of-molecule.at)
-
-# (un)select atoms by element
-m4_include(Selection/Atoms/AtomByElement/testsuite-selection-atoms-by-element.at)
-
-# (un)select atom by id
-m4_include(Selection/Atoms/AtomById/testsuite-selection-atom-by-id.at)
-
-# (un)select atoms inside cuboid
-m4_include(Selection/Atoms/AtomsInsideCuboid/testsuite-selection-atoms-inside-cuboid.at)
-
-# (un)select atoms inside sphere
-m4_include(Selection/Atoms/AtomsInsideSphere/testsuite-selection-atoms-inside-sphere.at)
-
-# clear atom selection
-m4_include(Selection/Atoms/ClearAtoms/testsuite-selection-clear-atoms.at)
+m4_include(Selection/Atoms/testsuite-selection-atoms.at)
 
 ### Molecules ###
 
-# (un)select all molecules
-m4_include(Selection/Molecules/AllMolecules/testsuite-selection-all-molecules.at)
-
-# clear molecule selection
-m4_include(Selection/Molecules/ClearMolecules/testsuite-selection-clear-molecules.at)
-
-# (un)select molecules by formula
-m4_include(Selection/Molecules/MoleculeByFormula/testsuite-selection-molecules-by-formula.at)
-
-# (un)select molecule by id
-m4_include(Selection/Molecules/MoleculeById/testsuite-selection-molecule-by-id.at)
-
-# (un)select molecule by name
-m4_include(Selection/Molecules/MoleculeByName/testsuite-selection-molecules-by-name.at)
-
-# (un)select molecule from atom selection
-m4_include(Selection/Molecules/MoleculeOfAtom/testsuite-selection-atoms-molecules.at)
+m4_include(Selection/Molecules/testsuite-selection-molecules.at)
