while-loop changes (more safe)