Fix bad writes on close.
Merge pull request #52 from pinktrink/master

Fix bad writes on close.