small simplifications and localization of variables.