1612 fix

TODO remove non-rf related commit.

This is my old fix for [issue 1612](https://code.google.com/p/sympy/issues/detail?q=1612&id=1612).  The original branch with some print statements is at https://github.com/asmeurer/sympy/tree/1612.  

I don't know if this has any issues. I'm opening this to get the tests to run.  If it works, I guess it can be merged. Note that this issue related to caching, so we should test it with the cache off too.