Revise get_key function and refactor