fix several race conditions in stats code