Better error handling, put shutdown in finally