refactor some tag-related code