Extracted generateKey function as method