Simplify the extraction algorithm. The state of the next pattern's node should be checked within the loop, not vice versa.