restructure slightly so that coverity check is cleaner