Fixed bugs in counting flags.