Rewrote do-while condition to make it more readable