#13 refactored the code to a separate function that returns state.