changed some variables to function local