Mechanical refactoring to replace state with messages