Added a check for opt.ref to avoide NoneType errors