Improved agi env parsing