Use single formatQuery method and fix relationship matching