big state machine and factory refactoring - not complete...