Rewrote the code for deriving Eq to make it work on more examples and fix some problems.