Rob: added comments and tidied up env checking