Refactored flag-related code.