Split environment handling out into a separate file and new header