magic envelope refactoring, more tests