substitute for-loops for while-loops where applicable