Upgrade SQL queries on tree.php