Envelope and envGen refactoring and bug fixes.