There seems to be a bit of too much overlap with TreeWriter.js

Can we refactor to share code?