matching pairs and inserting