fix memory issues. Cleaner shutdown process.