first version of better useful buf
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index a0cc9b6..d98ebd5 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -57,16 +57,14 @@
 /*
    Public function -- see UsefulBuf.h
  */
-int UsefulBuf_Copy(UsefulBuf *pDest, const UsefulBufC Src)
+UsefulBufC UsefulBuf_Copy(UsefulBuf Dest, const UsefulBufC Src)
 {
-   if(Src.len > pDest->len)
-      return 1;
+   if(Src.len > Dest.len)
+      return NULLUsefulBufC;
    
-   memcpy(pDest->ptr, Src.ptr, Src.len);
-   
-   pDest->len = Src.len;
-   
-   return 0;
+   memcpy(Dest.ptr, Src.ptr, Src.len);
+    
+   return((UsefulBufC){Dest.ptr, Src.len});
 }
 
 /*
@@ -121,7 +119,8 @@
  
  THIS FUNCTION DOES POINTER MATH
  */
-void UsefulOutBuf_Init(UsefulOutBuf *me, void *pStorage, size_t uStorageSize)
+#if NODEF
+void UsefulOutBuf_InitOld(UsefulOutBuf *me, void *pStorage, size_t uStorageSize)
 {
    me->magic  = USEFUL_OUT_BUF_MAGIC;
    UsefulOutBuf_Reset(me);
@@ -141,6 +140,27 @@
       me->err = 1;
 #endif
 }
+#endif
+
+void UsefulOutBuf_Init(UsefulOutBuf *me, UsefulBuf Storage)
+{
+    me->magic  = USEFUL_OUT_BUF_MAGIC;
+    UsefulOutBuf_Reset(me);
+    me->UB     = Storage;
+    
+    // The following check fails on ThreadX
+#if 0
+    // Sanity check on the pointer and size to be sure we are not
+    // passed a buffer that goes off the end of the address space.
+    // Given this test, we know that all unsigned lengths less than
+    // me->size are valid and won't wrap in any pointer additions
+    // based off of pStorage in the rest of this code.
+    const uintptr_t ptrM = UINTPTR_MAX - uStorageSize;
+    if(pStorage && (uintptr_t)pStorage > ptrM) // Check #0
+        me->err = 1;
+#endif
+}
+
 
 
 /*
@@ -197,7 +217,7 @@
    // Make sure valid data is less than buffer size. This would only occur
    // if there was corruption of me, but it is also part of the checks to
    // be sure there is no pointer arithmatic under/overflow.
-   if(me->UB.len > me->size) {  // Check #1
+   if(me->data_len > me->UB.len) {  // Check #1
       me->err = 1;
       return; // Offset of valid data is off the end of the UsefulOutBuf due to uninitialization or corruption
    }
@@ -223,7 +243,7 @@
    uint8_t *pSourceOfMove       = ((uint8_t *)me->UB.ptr) + uInsertionPos; // PtrMath #1
    size_t   uNumBytesToMove     = me->UB.len - uInsertionPos; // PtrMath #2
    uint8_t *pDestinationOfMove  = pSourceOfMove + NewData.len; // PtrMath #3
-   size_t   uRoomInDestination  = me->size - (uInsertionPos + NewData.len); // PtrMath #4
+   size_t   uRoomInDestination  = me->UB.len - (uInsertionPos + NewData.len); // PtrMath #4
    
    if(uNumBytesToMove && me->UB.ptr) {
       memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove);
@@ -231,11 +251,11 @@
    
    /* 4. Put the new data in */
    uint8_t *pInsertionPoint = ((uint8_t *)me->UB.ptr) + uInsertionPos; // PtrMath #5
-   uRoomInDestination       = me->size - uInsertionPos; // PtrMath #6
+   uRoomInDestination       = me->UB.len - uInsertionPos; // PtrMath #6
    if(me->UB.ptr) {
       memmove(pInsertionPoint, NewData.ptr, NewData.len);
    }
-   me->UB.len += NewData.len ;
+   me->data_len += NewData.len ;
 }
 
 
@@ -267,24 +287,31 @@
 
 
 /*
- Public function -- see UsefulBuf.h
- 
- Returns the resulting valid data in a UsefulBuf
- 
+ Public function -- see UsefulBuf.h 
  */
-int UsefulOutBuf_OutUBuf(UsefulOutBuf *me, UsefulBuf *O)
+UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *me)
 {
    if(me->err) {
-      return me->err;
+      return NULLUsefulBufC;
    }
    
    if(me->magic != USEFUL_OUT_BUF_MAGIC) {
       me->err = 1;
-      return 1;
+      return NULLUsefulBufC;
    }
-   
-   *O = me->UB;
-   return 0;
+    
+    return(UsefulBufC){me->UB.ptr,me->data_len};
+}
+
+
+UsefulBufC UsefulOutBuf_CopyOut2(UsefulOutBuf *me, UsefulBuf pDest)
+{
+    UsefulBufC Tmp = UsefulOutBuf_OutUBuf(me);
+    if(UsefulBuf_IsNULL(Tmp)) {
+        return NULLUsefulBufC;
+    }
+    
+    return UsefulBuf_Copy(pDest, Tmp);
 }
 
 
@@ -296,18 +323,12 @@
  */
 int UsefulOutBuf_CopyOut(UsefulOutBuf *me, void *pBuf, size_t uBufSize, size_t *puCopied)
 {
-   UsefulBuf B;
-   if(UsefulOutBuf_OutUBuf(me, &B)) {
-      return 1; // was in error state or was corrupted
-   }
-   
-   if(B.len > uBufSize) {
-      return 1; // buffer was too small
-   }
-   
-   memmove(pBuf, B.ptr, B.len);
+    UsefulBufC B = UsefulOutBuf_CopyOut2(me, (UsefulBuf){pBuf, uBufSize});
+    if(UsefulBuf_IsNULL(B)) {
+        return 1; // was in error state or was corrupted or pBuf too small
+    }
 
-   *puCopied = me->UB.len;
+   *puCopied = B.len;
    
    return 0;
 }
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 90d3bf3..25c62f7 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -339,9 +339,8 @@
    int nReturn = QCBOR_ERR_HIT_END; 
    
    if(pBytes != NULL) {
-      pDecodedItem->val.string.ptr = pBytes;
-      pDecodedItem->val.string.len = uNumber;
-      pDecodedItem->uDataType      = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING;
+      pDecodedItem->val.string = (UsefulBufC){pBytes, uNumber};
+      pDecodedItem->uDataType  = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING;
       nReturn = QCBOR_SUCCESS;
    }
    
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 14e13e8..b40db6c 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -193,13 +193,13 @@
 /*
  Public function for initialization. See header qcbor.h
  */
-void QCBOREncode_Init(QCBOREncodeContext *me, void *pBuf, size_t uBufLen)
+void QCBOREncode_Init(QCBOREncodeContext *me, UsefulBuf Storage)
 {
    memset(me, 0, sizeof(QCBOREncodeContext));
-   if(uBufLen > UINT32_MAX) {
+   if(Storage.len > UINT32_MAX) {
       me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
    } else {
-      UsefulOutBuf_Init(&(me->OutBuf), pBuf, uBufLen);
+      UsefulOutBuf_Init(&(me->OutBuf), Storage);
       Nesting_Init(&(me->nesting));
    }
 }
@@ -604,8 +604,9 @@
       me->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
       goto Done;
    }
-   
-   UsefulOutBuf_OutUBuf(&(me->OutBuf), &(pEncodedCBOR->Bytes));
+
+   pEncodedCBOR->Bytes = UsefulOutBuf_OutUBuf(&(me->OutBuf));
+
    pEncodedCBOR->uItems = Nesting_GetCount(&(me->nesting));
    
 Done: