Improves while loop logic