refactoring and shutdown (untested)