Fixed state reading.

The state is provided even in case of error, so checking for error
before reading state is removed.
Also provided error handling in case of not found code.