envremap code from https://github.com/rlk/envtools.