Changed removeNodes to extract logic from the Listener.