Refactor alert code