Adding test cases and debug output, state display etc.
Fixed errors.