total revamp of flag function