fixed shutdown function