fix shutdown, refactor