Fixed escaping issues

Now we process each substitution exactly once in one pass,
avoiding the problem when trying to escape positional or named
substitutions.