Refactor codgen