You should fix this line.  Both atoms() and free_symbols doesn't have too much sense here.

The current code in this if branch is an equivalent for `return x not in coeff.free_symbols`.