Tag algorithm and data structure