move logic into while test and get rid of breaks