function should also check the directory and set permissions