Add tests, fix tag replacement