refactored code to set environment, made environment use optional