This function also needs to unregister previous listeners, if any.