Fixes many bugs; adds query functionality, including tree query