Added piece counting function