The check `key.startswith('<') and key.endswith('>') is repeated many times in code to check for a special key. Replace all these with a call to the same function.