fixing variable scopes