Fixed some var::equals ordering problems, and added unit tests for this.