fixed cbor sequence bug
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index de5d77b..13be1f7 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -131,19 +131,13 @@
    }
 }
 
-// Determine if at the end of a map or array, taking into
-// account map mode. If this returns true, it is OK
-// to get another item.
+// Determine if at the end of a map or array while in map mode
 inline static bool
 DecodeNesting_AtEnd(const QCBORDecodeNesting *pNesting)
 {
-   if(DecodeNesting_IsAtTop(pNesting)){
-      // Always at end if at the top level of nesting
-      return true;
-   }
-
    if(pNesting->pCurrent->uMapMode) {
       if(pNesting->pCurrent->uCount == 0) {
+         // TODO: won't work for indefinite length
          // In map mode and consumed all items, so it is the end
          return true;
       } else {
@@ -151,7 +145,7 @@
          return false;
       }
    } else {
-      // Not in map mode, and not at top level so it NOT the end.
+      // Not in map mode. The end is determined in other ways.
       return false;
    }
 }
@@ -1173,14 +1167,24 @@
 
    QCBORError nReturn;
 
-   // Check if there are an TODO: incomplete comment
+   /* For a pre-order traversal a non-error end occurs when there
+    are no more bytes to consume and the nesting level is at the top.
+    If it's not at the top, then the CBOR is not well formed. This error
+    is caught elsewhere.
+
+    This handles the end of CBOR sequences as well as non-sequences. */
    if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0 && DecodeNesting_IsAtTop(&(me->nesting))) {
       nReturn = QCBOR_ERR_NO_MORE_ITEMS;
       goto Done;
    }
-   
+
+   /* It is also and end of the input when in map mode and the cursor
+    is at the end of the map */
+
+
    // This is to handle map and array mode
-   if(UsefulInputBuf_Tell(&(me->InBuf)) != 0 && DecodeNesting_AtEnd(&(me->nesting))) {
+   if(DecodeNesting_AtEnd(&(me->nesting))) {
+//   if(UsefulInputBuf_Tell(&(me->InBuf)) != 0 && DecodeNesting_AtEnd(&(me->nesting))) {
       nReturn = QCBOR_ERR_NO_MORE_ITEMS;
       goto Done;
    }
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index bd35fde..9913e48 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -3923,7 +3923,54 @@
    if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
       return 1009;
    }
-   
+
+   // These tests confirm the cursor is at the right place after entering a map or array
+
+   // Confirm cursor is at right place
+   QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
+   QCBORDecode_EnterMap(&DCtx);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   if(Item1.uDataType != QCBOR_TYPE_INT64) {
+      return 2001;
+   }
+
+
+   QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_EnterArray(&DCtx);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
+      return 2002;
+   }
+
+   QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
+   QCBORDecode_EnterMap(&DCtx);
+   //QCBORDecode_GetNext(&DCtx, &Item1);
+   //QCBORDecode_GetNext(&DCtx, &Item1);
+   //QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
+      return 2003;
+   }
+
+   QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
+   QCBORDecode_EnterMap(&DCtx);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
+   QCBORDecode_GetNext(&DCtx, &Item1);
+   if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
+      return 2003;
+   }
+
+
    return 0;   
 }