New contracts

This is the new version of contract generation that allows for optizations of the contracts to remove unneeded parts. This code also contains a bunch of other refactorings that I found were needed, but not should be split out before I check this in.