Fix recursive output tree structure + add some unit tests