Changed I/O from if to while