refactored state tree logic into separate class