Add code from go branch of cod