add recursive pairing function