And another attempt (a little bit different code approach) to solve this issue, which I think broke on some API change from version 2.5+
but still, could be interesting to look at (attaching)
and, was based on some work done by Alex Smith as part of the Serving Mathematics project (http://maths.york.ac.uk/serving_maths)