refactor env escaping

add unit tests