Finish the query main function