A bit more refactoring: set up state before mutating anything