added some code to handle state changes when user uses device buttons