Fixed some bugs and optimised some parts of the binary tree