Changed code to use less crash-prone ways of changing states