Fix bug in saveState() and restoreState() + improve error handling
Signed-off-by: Karel Goderis <karel.goderis@me.com>