Buffer counting function