Fewer pushd-popd's and got rid of a nested if.