Refactor most of macro logic into functions in `internal`