Conditional write

Closes #156 (see discussion there).