Factor out the write operations.

This fixes the wrong sign and missing node number in the last node.