refactered key function