refactoring startup/shutdown code to make it more DRY