Refactoring compiler.

Not entirely happy with this yet, but it's getting better.