externalizing variables and scope improvements