Refactor appDelegate