Fixed tree implementation and introduced reference counting
Fixed compile issue