Improved startup and shutdown logic