Merged tests and fixes with Jeremy's work. Moved some helper functions into expr.py.