improve error handling to fail on non-zero error code