Fix error code handling with chdir()