Extract write() method.