Error handling: some fixes, unittests