Updated script to create folders if necessary