Changed to use opendir, etc