Refactored to improve legibility of generation logic