Extract macroexpansion logic