Codegen refactor using pattern matching.