added some checks to prevent IndexError
improved get_env() method