Optimized the 'while' loop.