Fix state flipping

fixes #796 and probably many more unsolved bugs

Elements must be first locked to ensure that no other thread uses them and then reloaded to ensure that recent changes are not lost.