refactored and improved statechart mechanism