most of the changes have been made to handle the \n as marker...
I think I've figured out the algorithm, but need to test later.