optimize computations
replace infinite loops with loops with counters