Added proper shutdown function