Fix double fclose and segfault and add comments in the scripting engine.