Fix error handling inside threads