updated find_envelope function