Extract common code into a macro