draft version of NAS code ported from gerrit review request