Fixed bugs in tree and main