modified collapse function to delete freed branch