changed for loops to decrementing while loops.