Updated build_query for mid