Perform all the writes in close

This fixes the bug reported in issue #64.