Fix handleRefChange so that KVs are only written on an actual change.