better exception handling + cleaner shutdown