Refactor to avoid busy wait for app start. Handle app exit events gracefully.