refactor query macro