cleanup code
replace while loops by for loops