improved tagging algorithm