Make uisoure optional, add cleanEnvironment option. Fixes #4398