transform recursive function in a while loop for reading rle