This code is ridiculous. First we get the total queue length and put it in the buffer. Then we rewind the buffer then we increment the buffer to the total queue length. Then we copy out the total queue length. How about we just use the total queue length directly!