Added function to replace state to fix bug in Chrome due to exitting/entering a state in the same event.