Sophisticated State Machine implementation
bug fixes