Refactored code to the State Pattern