replaced copytree with a loop