Add error handling (possible segfaults on error).