Thread safety fix from Steve Ash