Modified code to work with key-value pairs