Support being given a directory for the output path (-p flag)

This should fix #656 and maybe a few other things.