Improve flag handling