Merge pull request #141 from nekron/refactors

Codegen refactor using pattern matching.