Refactored machine to use a fold rather than loop and variable.