renaming res and changing variable scopes