Reworked the scripting interface, added cleanup on shutdown.