Refactor buildQueryUrl method