Fix logic bugs in output directory calculation.