changed the loop to while loop to make use of instruction pointer