Add a minimization algorithm and code generation. Lots of new things and fixes. Code cleanup.