refactored code into different states