Fixed race condition caused by use of streams