Refactured key storage functions