Revert code to while-loops, add comments