Semantics of this function wasn't really clear and it was used only once, so just inline it at the point of use and define better what happens for various states there. Also use a switch rather than testing for individual states to make sure this code is updated if another state is added in the future. No real changes.