Rewrite algorithms to be more clearly based on streams. https://www.w3.org/Bugs/Public/show_bug.cgi?id=16688