check for nonexistent directory earlier
change from cwd to getcwd for better stability