Refactoring/streamline env detection

Another bunch of assorted cleanups. Will it ever end?