Added flag to eliminate race conditions.