refactored a bunch of code ready for streamgraph integration