Modified code to reflect the new listener logic.