Added function to group pairs into sequences