Fix generate for better memory use

Don't allocate space unless its needed.