Move a lot the code from gui.c to librarytree.c