Rewrite codegen according to issue #7

Code and errors are now being output to the state monad.