Revert "Revert "while loop causing infinite loop""

This reverts commit a9ff7c6125ec886c893b70041e47238c714e232e.