Improve environment detection