Updated to capture exit code properly