Fixed do/while loop logic