Fixed to handle exit code correctly