Use dict.get() for safe key retrieval and minor code refactoring