Rewrite our Equals implementations

Required for working Lua support. This is the first of a small series of Lua prs.
Fixes #4088