Refactor common generator code