Converted to golang (partially).