Intial handeling of shutting down
Clean up the code and add some logging