Refactor flag code