refactor appdelegate