Refactor tag related code