Refactor of tag setting logic