Fixed state transition error. Added copyright. Some refactoring.
Fixed state transition error