Fix many bugs related to closing handles, add a lua wrapper around the ugly raw functions, update test file.

Then, everything changed when ...