You can refactor the event dispatching to listeners in a separate function called from here.