Replaces for with while loop