Merged with new envelope code