limit scope of variables, fix printing with extra NULL