factored out ensureDirectories and added parent method