Refactored flag/unflag functions using a HOF