diff --git a/.gitattributes b/.gitattributes
index 89bff0e37e1836ab0478f1ff3de531c1d40ec1e8..6d505c80d4143859ec27424c19670ff405d2022a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -3642,6 +3642,7 @@ RTCP/InputProc/src/newInputSection.cc -text
 RTCP/InputProc/src/shmtest.cc -text
 RTCP/InputProc/test/CMakeLists.txt -text
 RTCP/InputProc/test/tRSPTimeStamp2.cc -text
+RTCP/InputProc/test/tRanges.cc -text
 RTCP/InputProc/test/tSharedMemory.cc -text
 RTCP/Interface/include/Interface/BGPAsm.h -text
 RTCP/Interface/include/Interface/BeamCoordinates.h -text
diff --git a/RTCP/InputProc/src/Ranges.h b/RTCP/InputProc/src/Ranges.h
index 7376c8bfaf159b0c36678de5b79d98284e952890..bf2856d9b6d9bef91db487ac1c3d03d4344f4c0d 100644
--- a/RTCP/InputProc/src/Ranges.h
+++ b/RTCP/InputProc/src/Ranges.h
@@ -31,6 +31,8 @@ public:
 
   SparseSet<int64> sparseSet( int64 first, int64 last ) const;
 
+  static size_t elementSize() { return sizeof(struct Range); }
+
 private:
   struct Range {
     // Write 'from' before 'to' to allow the following invariant:
@@ -110,7 +112,7 @@ void Ranges::excludeBefore( int64 to )
       continue;
     }
 
-    if (i->from > to) {
+    if (i->from < to) {
       // shorten
       i->from = to;
     }
@@ -138,8 +140,8 @@ bool Ranges::include( int64 from, int64 to )
   // new range is needed
   struct Range * const next = head + 1 == end ? begin : head + 1;
 
-  if (next->to < to - minHistory) {
-    // range at 'next' is old enough to toss away
+  if (next->to == 0 || next->to < to - minHistory) {
+    // range at 'next' is either unused or old enough to toss away
     next->from = from;
     next->to   = to;
 
diff --git a/RTCP/InputProc/test/CMakeLists.txt b/RTCP/InputProc/test/CMakeLists.txt
index 152757b40d7611d2df06aa6dfb9fe9db8bc0d9eb..baf082535ccb3aa14b39b2ebbf6ad25d897c703a 100644
--- a/RTCP/InputProc/test/CMakeLists.txt
+++ b/RTCP/InputProc/test/CMakeLists.txt
@@ -6,4 +6,5 @@ include(LofarCTest)
 include_directories(${PACKAGE_SOURCE_DIR}/src)
 
 lofar_add_test(tRSPTimeStamp2 tRSPTimeStamp2.cc)
+lofar_add_test(tRanges tRanges.cc)
 lofar_add_test(tSharedMemory tSharedMemory.cc)
diff --git a/RTCP/InputProc/test/tRanges.cc b/RTCP/InputProc/test/tRanges.cc
new file mode 100644
index 0000000000000000000000000000000000000000..795bd912d6a1d38205fc1b1fb0e712ea0cdd8185
--- /dev/null
+++ b/RTCP/InputProc/test/tRanges.cc
@@ -0,0 +1,74 @@
+#include <lofar_config.h>
+#include "Ranges.h"
+#include <Common/LofarLogger.h>
+#include <Common/Thread/Thread.h>
+#include <unistd.h>
+#include <vector>
+
+using namespace LOFAR;
+using namespace RTCP;
+using namespace std;
+
+int main( int, char **argv ) {
+  INIT_LOGGER( argv[0] );
+
+  size_t clock = 200 * 1000 * 1000;
+  bool result;
+
+  {
+    LOG_INFO("Basic tests");
+
+    vector<char> buf(10 * Ranges::elementSize());
+    Ranges r(&buf[0], buf.size(), clock / 1024, true);
+
+    result = r.include(10, 20);
+    ASSERT(result);
+
+    /* r == [10,20) */
+
+    ASSERT(r.anythingBetween(10, 20));
+    ASSERT(r.anythingBetween(0, 30));
+    ASSERT(r.anythingBetween(0, 15));
+    ASSERT(r.anythingBetween(15, 30));
+    ASSERT(!r.anythingBetween(0, 10));
+    ASSERT(!r.anythingBetween(20, 30));
+
+    result = r.include(30, 40);
+    ASSERT(result);
+
+    /* r == [10,20) + [30,40) */
+
+    SparseSet<int64> s(r.sparseSet(0,100));
+    ASSERT(!s.test(9));
+    ASSERT(s.test(10));
+    ASSERT(s.test(11));
+    ASSERT(s.test(19));
+    ASSERT(!s.test(20));
+    ASSERT(!s.test(29));
+    ASSERT(s.test(30));
+    ASSERT(s.test(31));
+    ASSERT(s.test(39));
+    ASSERT(!s.test(40));
+
+    SparseSet<int64> s2(r.sparseSet(15,35));
+    ASSERT(!s2.test(14));
+    ASSERT(s2.test(15));
+    ASSERT(s2.test(19));
+    ASSERT(!s2.test(20));
+    ASSERT(!s2.test(29));
+    ASSERT(s2.test(30));
+    ASSERT(s2.test(31));
+    ASSERT(!s2.test(35));
+
+    r.excludeBefore(35);
+
+    /* r == [35,40) */
+
+    ASSERT(!r.anythingBetween(0,35));
+    ASSERT(r.anythingBetween(35,40));
+    ASSERT(!r.anythingBetween(40,100));
+  }
+
+
+  return 0;
+}