move the key variable after if statement