Fix bug in ReplaceNamedGroup + added tests.