improve logic for app's background mode