Changed loop to use while loop and fixed error in documentation