looping and some more logic