Cleaned up environment detection