Move alert-triggering code to subfunction