Rename environment middleware function.
Improve env middleware assignment logic.