Rewrote a lot of the code, using kiama now for rewriting and pretty-printing