Flags refactoring and fixes