Calculate root dir more efficiently