Change while to a forEach loop