Some refactoring, new state update logic