ts_close segfault fix

Hi there,

This patch fixes a segfault when ts_close() is called and more than one module is attached. I changed the loop through the linked list of modules to save the "next" pointer before calling fini().