working algorithm for rst segment extraction