refactored gen code