Fixed some bugs with flag code