Refactor CodeGenerator.

By using State monad!