Added EOF tag
Before merging, add an EOF tag to the end of the stream. Then look for that EOF tag in the extraction loop and break out of the loop if found.