added flag passing logic