Fixed state transitions.
Fixed memory leak.