restructured for loops, now using while loops and 'read'