Include defensive check against `nil` env.

Also include a test to ensure that the code works ;)