Fixed handling of errors, avoid core dumps.