Fix open and close

Fix race condition on open and try to clean up properly on close