improve controller script to do proper shutdowns