Refactor flag handling