Since things are done with symbol sets below where you have made changes, I think it would be better to just unify the symbols here at the outset and then let everything run as written (i.e. without the changes you made):

```
free_symbols = set([Symbol(s.name) for s in expr.free_symbols])
range_symbols = set([Symbol(S(t[0]).name) for t in args])
```

(By using S() above, you can then pass strings as symbols, e.g. a range could be ('x', 1, 3).)