Several fixes, especially in rewriteExpression

The basic tests are passing now. The last test output differs as it prints out
an array as text.