Refactor alert and fix bugs