modified GetRoot function to be iterative instead of recursive