Refactored: key events are now stored in a map