Initial refactoring of write() to handle more types.