Refactored state management and started putting in tests