On closer inspection is isn't that difficult at all to store the previous states. See updated patch.