make write synchronized, so that we do not get weird errors.  Fixes #665