added check to make sure that target directory exists