Change from 'while' to 'for' loop