added writetree and some tests