fixes up segfaults, makes close() idempotent, fixes #10