state machine callbacks refactored and optimized