added if-then and looping logic