Refactoring Rama using a state machine