Add write buffering, code cleanup.