Refactoring/streamline env detection

Various cleanups