Refactored to break up generation into smaller chunks