added flag to prevent race conditions