piecewise tree structure  + writer working