Refactoring to store keys in a dictionary